我正在使用 UITableView ,我正在做的是在cellForRowAt使用UITableView 的didSelectRow函式點擊單元格時更改單元格的顏色。困擾我的是當我向下滾動或向上滾動時,我之前更改顏色的那些單元格被更改為其他單元格。這是我的代碼:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "TasksTableViewCell") as! TasksTableViewCell
cell.backView.backgroundColor = .white
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = myTableView.cellForRow(at: indexPath) as! TasksTableViewCell
cell.backView.backgroundColor = UIColor(named: "primaryViewColor")
}
有誰知道為什么會這樣?有沒有人有一個解決方案,當只有那些我點擊的單元格改變顏色時,當我向下滾動或向上移動時,只有這些單元格有其他顏色?
uj5u.com熱心網友回復:
每次顯示該單元格時都會呼叫 cellForRowAt。您需要選定的串列來保存選定的索引。
var listSelected: [Int] = []
和
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TasksTableViewCell") as! TasksTableViewCell
cell.backView.backgroundColor = listSelected.contains(indexPath.row) ? UIColor(named: "primaryViewColor") : .white
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if listSelected.contains(indexPath.row) {
listSelected = listSelected.filter{$0 != indexPath.row}
} else {
listSelected.append(indexPath.row)
}
tableView.reloadRows(at: [indexPath], with: .automatic)
}
uj5u.com熱心網友回復:
我遇到過很多次你看到的問題。即使使用和 iVar 可以解決問題,您也是在混合“控制器”邏輯和“模型”邏輯。
我通常更喜歡在模型內移動“選擇”狀態。假設您有一個“聯系人”類用于填充單元格資料(通常的 MVC 模式),我添加:
class contact{
..
var selected = false
}
并且在電視委托方法中我用來應用選擇,或者更好的是我在自定義單元格中使用自定義選擇方法(例如在單元格中查看 √ 元素)作為獎勵,多重選擇是免費的,您還可以保存當前選擇下次運行:)
uj5u.com熱心網友回復:
因此,據我了解,您選擇了一個單元格,然后其他單元格看起來像是被選中的?
如果是這樣,我認為這是因為您更改了單元格的背景顏色,而 tableViews 和 collectionViews 正在重用單元格,基本上將您更改的背景保留在后面。
uj5u.com熱心網友回復:
TableViewCell 離開可見區域后會立即被重用。這意味著一旦向上或向下滾動,您為其背景著色的單元格將從視圖層次結構中洗掉。如果再次滾動相應的行,cellForRowAt則再次為此 IndexPath 呼叫該函式,并且單元格將獲得白色背景。
最簡單的方法是保存選定單元格的 IndexPaths 并在cellForRowAt必須選擇當前單元格時簽入函式。
將以下 var 添加到 viewController 類:
var selectedIndexPaths = Set<IndexPath>()
并修改 tableView 委托方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = myTableView.dequeueReusableCell(withIdentifier: "TasksTableViewCell") as! TasksTableViewCell
cell.backView.backgroundColor = (selectedIndexPaths.contains(indexPath) ? UIColor(named: "primaryViewColor") : .white)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if selectedIndexPaths.contains(indexPath)
{
selectedIndexPaths.remove(indexPath)
}
else
{
selectedIndexPaths.insert(indexPath)
}
tableView.reloadRows(at: [indexPath], with: .none)
}
uj5u.com熱心網友回復:
您可以使用
第一步:創建模型
class DemoModel {
var isSelected: Bool = false
var color: UIColor = .While
}
第2步:在tableview中
var listDemo: [DemoModel] = [DemoModel(),...]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier:
"TasksTableViewCell") as! TasksTableViewCell
var obj = listDemo[indexPath.row]
cell.backView.backgroundColor = obj.color
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var obj = listDemo[indexPath.row]
obj.color = UIColor(named: "primaryViewColor")
tableView.reloadRows(at: [indexPath], with: .automatic)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405072.html
標籤:
