如何在特定位置快速更新tableview的项目?

时间:2020-07-16 05:41:53

标签: ios swift uitableview uikit

我的应用程序中具有tableview,单击此列表中的任何项目后,我将移至该功能的详细信息视图之类的地方

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.tabBarController?.tabBar.isHidden = true
        
        
        let storyboard = UIStoryboard(name: "Fruitslist", bundle: nil)
        guard let details = storyboard.instantiateViewController(identifier: "DetailView") as? DetailView else { return }
        
        
        details.modalPresentationStyle = .fullScreen
        details.delegate = self
        
        self.show(details, sender: nil)
        
        
    }

在这个详细信息视图中,我有协议:

protocol AddToNotepad {
    func addToNotepadFunc(added:Bool)
}

当我从此详细信息屏幕移至主列表时,我会发送一些数据:

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        if self.isMovingFromParent {
            self.tabBarController?.tabBar.isHidden = false
            self.delegate?.addToNotepadFunc(added: true)
        }
    }

在我的主列表中,我以扩展名收到此数据:

extension FruitList:AddToNotepad{
    func addToNotepadFunc(added: Bool) {
        let indexPath = IndexPath(item: 1, section: 2)
        if let fruitCell = tableView.cellForRow(at: indexPath) as? FruitListCell {
            
            fruitCell.eTitle.text = "232332322332"
        }
    }
    
}

,我不了解如何在某些部分中更新单元格的某些部分。我也可以发送详细视图的部分,但是如何在我不理解的特定部分上更新单元格的某些元素:(您可以看到我的尝试,但是它不起作用。也许有人知道我在哪里出错了?

1 个答案:

答案 0 :(得分:1)

您的问题是您在此处设置了静态索引路径

let indexPath = IndexPath(item: 1, section: 2)

您应该

var lastIndex:IndexPath!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  lastIndex = indexPath
  .....
}

然后使用它来更新addToNotepadFunc内部的模型,例如

array[lastIndex.section][lastIndex.row].added = true
// refresh table at that indexpath
self.tableView.reloadRows(at:[lastIndex],with:.none)