使用自定义按钮 - Swift 3从表视图中删除一行

时间:2022-09-26 11:01:33

How to delete a row from tableview using custom buttom

如何使用自定义按钮从tableview中删除行

//CustomCell.swift

protocol FavoriteCellDelegate {
    func deleteButton(sender:CustomCell)
}

class FavoriteItemTableViewCell: UITableViewCell{
    var delegate: FavoriteCellDelegate!
    @IBAction func deleteButton(_ sender: UIButton) {
    delegate.deleteButton(sender: self)
}
}


CustomClass:UITableViewDataSource,UITableViewDelegate,CustomCellDelegate{
@IBOutlet weak var tableView: UITableView!
// all necessary functions for table view....

// Function delegated to perform action.
func deleteButton(sender:FavoriteItemTableViewCell){
    //How should I delete. How can I get index path here
}

}

}

Q. What should I write in the deleteButton function ? I am unable to get the indexPath here so what should I do instead. I already have another button in cell and the delegation is working fine.

问:我应该在deleteButton函数中写什么?我无法在这里获取indexPath,所以我该怎么做。我已经在单元格中有另一个按钮,代表团工作正常。

1 个解决方案

#1


1  

you can get indexPath using table view point like this

你可以像这样使用表视点来获取indexPath

let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: tableview)
let indexPath = tableview.indexPathForRow(at: buttonPosition)

#1


1  

you can get indexPath using table view point like this

你可以像这样使用表视点来获取indexPath

let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: tableview)
let indexPath = tableview.indexPathForRow(at: buttonPosition)