簡體   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