繁体   English   中英

查看时的UITableView重新加载部分

[英]UITableView reload section while viewing

在一个自定义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)

我避免在代码中调用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"]

@Manali建议执行的操作,调用tableView.reloadSections触发对cellForRowAtIndexPath的一系列调用,在该方法内部,出队的单元格可能与通过调用cellForRow得到的对象不完全相同,正确的方法是存储有关隐藏/取消隐藏,以及显示/隐藏cellForRowAtIndexPath内部的图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM