在我的一个自定义TableViewCells上按下按钮后,我想在其他自定义TableViewCells上隐藏/取消隐藏图像。我的细胞按部分排序。
如果按下按钮,切换到另一个视图,然后返回到我的TableView,更改将生效。我希望无需切换视图即可立即显示更改。
我的代码:
let otherCellIndexPath = IndexPath(item: 0, section: 0)
let otherCell = tableView.cellForRow(at: oldIndexPath) as! CustomTableViewCell
otherCell.hideImage()
let currentCell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
currentCell.showImage()
tableView.beginUpdates()
tableView.reloadSections([otherCellIndexPath,indexPath], with: .none)
tableView.endUpdates()
我错过了什么? (使用Xcode 8 b6,Swift 3)
答案 0 :(得分:1)
我避免在我的代码中调用cellForRow。相反,我维护一个字典,用于每个单元格的状态,如
var dictSection0 = [["ImageName" : "SampleImageSection0Row0","hidden":true],["ImageName" : "SampleImageSection0Row1","hidden":false]]
var dictSection1 = [["ImageName" : "SampleImageSection1Row0","hidden":true],["ImageName" : "SampleImageSection1Row1","hidden":false]]
var tableDataModel = [dictSection0,dictSection1]
在按钮上点击我会修改字典。在上面的案例设置中,对于第0部分的第1行隐藏为true
var rowDict = tableDataModel[0][1]
rowDict["hidden"] = true
然后重新加载整个表(或者你正在做的一部分)以反映更改。
在cellForRow atIndexPath方法中,该字典用于显示/隐藏单元格中的图像。
var rowData = tableDataModel[indexPath.section][indexPath.row]
let tableCell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath)
tableCell.imageView.hidden = rowDict["hidden"]
答案 1 :(得分:1)
@Manali建议应该做什么,调用tableView.reloadSections会触发2015CSE3245(“John”, “1997-08-13”, “73”, “176”);
上的一系列调用,在该方法中,出列的单元格可能与通过调用{{1}获得的对象完全相同},正确的方法是存储有关隐藏/取消隐藏的信息,并在cellForRowAtIndexPath