从UITableViewCell的accessoryView中的自定义按钮获取索引路径

时间:2017-12-06 07:23:26

标签: ios swift uitableview uisearchcontroller

我有一个UISearchControllerUITableViewControllersearchResultsController分开。

class SearchResultsViewController: UITableViewController {

    var fruits: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }

    @objc func addFruit(_ sender: UIButton) {
        let point = tableView.convert(sender.bounds.origin, to: sender)
        let indexPath = tableView.indexPathForRow(at: point)
        print(indexPath?.row)
    }

    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fruits.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = fruits[indexPath.row]
        cell.selectionStyle = .none

        let addButton = UIButton(type: .custom)
        addButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        addButton.setImage(UIImage(named: "add"), for: .normal)
        addButton.contentMode = .scaleAspectFit
        addButton.addTarget(self, action: #selector(addFruit(_:)), for: .touchUpInside)
        addButton.sizeToFit()
        cell.accessoryView =  addButton

        return cell
    }

}

我需要在显示搜索结果的单元格中显示自定义按钮。所以我添加了UIButton作为单元格accessoryView。它外观和工作正常。

enter image description here

现在,当用户点按此按钮时,我需要获取单元格indexPath

我试图让它如下所示。

@objc func addFruit(_ sender: UIButton) {
    let point = tableView.convert(sender.bounds.origin, to: sender)
    let indexPath = tableView.indexPathForRow(at: point)
}

但它会为每个单元格返回nil

还有其他方法可以从自定义按钮点按indexPath吗?我在这里也添加了demo project

4 个答案:

答案 0 :(得分:1)

创建一个名为SOButton的自定义类,并为其添加IndexPath类型的变量。使用此类进行添加按钮初始化。

//Your class will look like - 

class SOButton: UIButton {
   var indexPath: IndexPath?
}

//Your action will look like -

@objc func addFruit(_ sender: SOButton) {
     print(sender?.indexPath.row)
}

//And in your cellForRow add

let addButton = SOButton(type: .custom)
addButton.indexPath = indexPath

希望这可以帮助你:)

答案 1 :(得分:0)

 let buttonPosition =  sender.convert(CGPoint.zero, to: self.tableView)
 let currentIndexPath = self.tableView.indexPathForRow(at: buttonPosition)

试试这个对我有用:)

答案 2 :(得分:0)

我建议使用这个简单的解决方案:将indexPath.row添加到按钮标记:

let addButton = UIButton(type: .custom)
addButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
addButton.tag = indexPath.row

按钮操作:

@objc func addFruit(_ sender: UIButton) {
    print(sender.tag)
}

答案 3 :(得分:0)

请更新您的代码。你在转换函数中传递UIButton发送者,请将tableView传递给它们

func getIndexPathByCgPoint(_ sender: UIButton) -> IndexPath? {
    let point = sender.convert(sender.bounds.origin, to: tableview)
    guard let indexPath = tableview.indexPathForRow(at: point) else {
        return nil
    }
    return indexPath
}

但是在节标题的情况下,它返回nil。

相关问题