简体   繁体   中英

How to delete specific row in tableview cell when tapping button in swift

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.

通过indexPath.row设置removeRowButton.tag ,现在您要设置cell.cellExpendButton.tag = indexPath.row ,默认情况下任何组件的标签为0

cell.removeRowButton.tag = indexPath.row 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM