我正在动态地将一个自定义单元格添加到UITableView中,我希望每个单元格都有一个按钮,可以从数据结构中删除该单元格并重新加载视图。
didSelectRowAt indexPath能够通过使用indexPath.section和indexPath.row从我的数据结构中删除元素,然后重新装入表格,但是如何通过触摸单元格内的按钮来做到这一点呢?
有人建议将逻辑移动到按钮的touchUpInside,但是我该怎么做呢?如何接收按下按钮的单元格的节和行,以了解要从数据结构中删除哪个元素?
发布于 2018-08-07 14:08:36
您可以将indexPath变量存储在自定义单元格对象中,并调用控制器上的函数。您需要在cellForRow(at:)函数中设置indexPath和delegate。
protocol CellButtonProtocol: class {
func cellButtonClicked(indexPath: IndexPath?)
}
class CustomCell: UITableViewCell {
weak var delegate: CellButtonProtocol?
var indexPath: IndexPath?
func buttonClick() {
delegate?.cellButtonClicked(indexPath: indexPath)
}
}https://stackoverflow.com/questions/51719794
复制相似问题