【问题标题】:Delete a row from table view using custom button - Swift 3使用自定义按钮从表格视图中删除一行 - Swift 3
【发布时间】:2017-06-23 19:27:23
【问题描述】:

如何使用自定义按钮从 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
}

}

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

【问题讨论】:

  • indexPathForCell 传入sender
  • 你必须使用委托吗?有更简单的方法可以满足此要求。
  • 我尝试使用 addTarget 并且它给出了一个异常然后我搜索并且stackoverflow上有人写道除了使用委托之外别无他法。

标签: swift xcode uitableview swift3 delegates


【解决方案1】:

您可以像这样使用表视图点获取 indexPath

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多