【问题标题】:How I can update item of tableview on certain position swift?如何快速更新某个位置的 tableview 项目?
【发布时间】:2020-07-16 05:41:53
【问题描述】:

我的应用程序中有表格视图,单击此列表中的任何项目后,我将移至此功能中的详细信息视图:

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"
        }
    }
    
}

而且我不明白如何在某些部分更新单元格的某些部分。我也可以发送详细视图的部分,但是如何在我不明白的部分更新单元格的某些元素:(你可以看到我的尝试,但它不起作用。也许有人知道我在哪里犯了一些错误?

【问题讨论】:

  • @Frankenstein,因为这是我造成的愚蠢错误:) 我认为这个问题对这个技能数据库没有帮助,但是如果你认为这个问题会,我当然可以恢复它有用

标签: ios swift uitableview uikit


【解决方案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)

【讨论】:

  • 我明白了,它有点像测试))当然我会设置选择的最后选择的索引,但是在详细屏幕上的一些操作后我无法更改所选单元格的标题,这是正确的问题现在,但感谢您指出另一个问题:)
  • @Andrew 我认为以上答案是针对您的整个问题的,如果还有其他问题,您能详细说明吗??
  • 是的,上面的答案是针对整个问题的,我正在 android recyclerview 上寻找像 notifyItemChanged(position,payload) 这样的东西 :) 我不需要刷新整个列表,但我想刷新某个单元格上的某个项目.也许这些信息会对你有所帮助:)
  • 你需要reloadRows 查看编辑,但首先你必须更新数据源
  • 那么,我看到如果不更新数据源就无法更新单元格?
猜你喜欢
  • 2015-06-11
  • 2020-10-02
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 2021-10-30
  • 2012-03-05
  • 1970-01-01
  • 2013-12-15
相关资源
最近更新 更多