2

I want to delete specific row when i tap a button in that cell. but here rows are deleting on the index base. if i tap button in fifth row button but 0th index row is deleting.

please help me.

here is my code:

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
    //cell.empImage.image = UIImage(named: countryArray[indexPath.row])
    cell.empName.text = namesArray[indexPath.row]

    cell.cellExpendButton.tag = indexPath.row
    cell.cellExpendButton.addTarget(self, action: #selector(expandcollapseButtonClicked), for: .touchUpInside)
    cell.removeRowButton.addTarget(self, action: #selector(removerowButtonClicked), for: .touchUpInside)

    return cell
} 

@objc func removerowButtonClicked(sender : UIButton!) {
    namesArray.remove(at: sender.tag)
    self.tableView.reloadData()
}

I want to remove cell in which button i tapped.

1
  • set cell.removeRowButton.tag = indexPath.row Commented May 16, 2019 at 10:52

1 Answer 1

4

Set removeRowButton.tag by indexPath.row, now you are setting cell.cellExpendButton.tag = indexPath.row, by default tag of any component is 0

cell.removeRowButton.tag = indexPath.row 
Sign up to request clarification or add additional context in comments.

2 Comments

Please note that this solution can fail if the rows of the table view can be deleted, inserted, or moved. It's best to avoid tags and determine the indexPath based on the location of the button in the view.
can u please answer this question stackoverflow.com/questions/56208548/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.