[英]How to delete specific row in tableview cell when tapping button in swift
當我點擊該單元格中的按鈕時,我想刪除特定的行。 但是這里的行是基於索引刪除的。 如果我點擊第五行按鈕中的按鈕,但第0個索引行正在刪除。
請幫我。
這是我的代碼:
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()
}
我想刪除我點擊了哪個按鈕的單元格。
通過indexPath.row
設置removeRowButton.tag
,現在您要設置cell.cellExpendButton.tag = indexPath.row
,默認情況下任何組件的標簽為0
cell.removeRowButton.tag = indexPath.row
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.