1

Hello in my tableView i added a refresh button to my custom tableViewCell

enter image description here

because i want to reload the single cell after the button tap, to do it (looking around the net) i created this func

 @IBAction func rowReload(_ sender: Any) {

        let index = IndexPath(row: , section: )
        self.tableView.reloadRows(at: [index], with: .none)
    }

but my problem is this line let index = IndexPath(row: , section: ), how can i tell to my function that the reload action must take place on the row where i pressed the button? (in simple words, what i have to write next row: and section: ?)

Hamish
  • 78,605
  • 19
  • 187
  • 280
fisherM
  • 177
  • 1
  • 3
  • 14

3 Answers3

0

In your cellForRow method assign tag to a button like shown below:

yourCell.yourButton.tag = indexPath.row

Then assign selector to it in cellForRow method.

yourCell.yourButton.addTarget(self, action: #selector(myClass.rowReload(_:)), forControlEvents: .TouchUpInside)

And in your rowReload method argument sender should have type UIButton and your method will be:

func rowReload(_ sender: UIButton) {

then you can access its tag and your IndexPath will be:

let index = IndexPath(row: sender.tag, section: 0)
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
0

In your method,

> "func tableView(_ tableView: UITableView, cellForRowAt indexPath:
> IndexPath) -> UITableViewCell"

you can give the tag number to the element (button) Eg.

cell.button.tag = indexPath

So, when you tap on the button, you can get the tag as its indexPath.

@IBAction func rowReload(_ sender: Any) {

    let index = sender.tag
    self.tableView.reloadRows(at: [index], with: .none) 

}

0

If there are multiple sections in UITableview then we cannot use tags to determine the indexpath.

    let indexPath = tableView.indexPathForSelectedRow
Rahul Dasgupta
  • 894
  • 6
  • 18