Andrew
Andrew

Reputation: 2157

How I can update item of tableview on certain position swift?

I have tableview in my app and after clicking any item from this list I move to smth like detail view in this function:

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

in this detail view I have protocol:

protocol AddToNotepad {
    func addToNotepadFunc(added:Bool)
}

when I move to main list from this detail screen I send some data:

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

in my main list I receive this data in extension:

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

and I don't understand how to update some part of cell in certain section. I also can send section in detail view but how to update certain element of cell on certain section I can't understand :( You can see my attempt but it doesn't work. Maybe someone knows where I did some errors?

Upvotes: 0

Views: 897

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

Your problem is that you set a static indexpath here

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

While you should

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

Then use it to update the model inside addToNotepadFunc like

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

Upvotes: 1

Related Questions