在单元格中获取indexPath.row

时间:2017-07-18 13:01:52

标签: ios swift uitableview

当我点击我的按钮时,我尝试在单元格中获取indexPath.row:

@IBAction func cellPlayButtonPressed(_ sender: Any) {
    let cell = (sender as AnyObject).superview??.superview as? UITableViewCell
    let indexPath = runnerTableView.indexPath(for: cell!)
    print(indexPath)
}

按下按钮时出现此错误:

  

致命错误:在解包可选值时意外发现nil

我已尝试在Stack Overflow上找到的所有内容,但没有任何工作。

让我知道这个错误在哪里!谢谢:)

5 个答案:

答案 0 :(得分:3)

只需复制给定的扩展名

extension UITableView {    
func indexPath(forItem: AnyObject) -> IndexPath? {
    let itemPosition: CGPoint = forItem.convert(CGPoint.zero, to: self)
    return self.indexPathForRow(at: itemPosition)
}}

使用:

@IBAction func cellPlayButtonPressed(_ sender: Any) {
    if let indexPath = runnerTableView.indexPath(forItem: sender) {
        print(indexPath)
    }
}

答案 1 :(得分:0)

试试这段代码:

@IBAction func cellPlayButtonPressed(_ sender: Any) {
    guard let button = sender as? UIButton else { return }
    let indexPath = tableView.indexPathForRow(at: button.center)
    print(indexPath)
}

答案 2 :(得分:-1)

我已经创建了demo并测试了它的工作方面。您可以使用如下:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

        let fruitName = fruits[indexPath.row]
        cell.textLabel?.text = fruitName
        cell.detailTextLabel?.text = "Delicious!"
        cell.imageView?.image = UIImage(named: fruitName)

        //Button added for click event
        let btn = UIButton.init(frame: CGRect(x: 10, y: 10, width: 100, height: 30))
        btn.backgroundColor = UIColor.red
        btn.setTitle("Click Here", for: UIControlState.normal)
        btn.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
        cell.addSubview(btn)

        return cell
    }

    func clickMe(sender : UIButton){

        let indexpath : NSIndexPath =  self.indexPathForCellContainingView(view: sender, inTableView: self.tableView)
        print("Current IndexPath Row : \(indexpath.row) Column : \(indexpath.section)")

    }

     func indexPathForCellContainingView(view: UIView, inTableView tableView: UITableView) -> NSIndexPath {
        let viewCenterRelativeToTableview = tableView.convert(CGPoint(x: view.bounds.midX, y: view.bounds.midY), from: view)
        let cellIndexPath = tableView.indexPathForRow(at: viewCenterRelativeToTableview)!
        return cellIndexPath as NSIndexPath
    }

答案 3 :(得分:-2)

cellForRow中设置单元格时,将按钮的tag属性设置为单元格的indexPath.row。然后将方法签名更改为以下内容:

@IBAction func cellPlayButtonPressed(_ sender: UIButton) {
    // sender.tag == indexPath.row
}

如果您的按钮是UIButton以外的其他类,则可以将其替换为您的班级。

答案 4 :(得分:-4)

当您处于cellForRowAtIndexPath时,可以为您的单元格发送indexPath。然后您可以访问indexPath。

func cellForRowAtIndex(_ tableView: UITableView, _ indexPath:IndexPath) -> UITableViewCell {

let UITableViewCell =  tableView.dequeueReusableCell(withIdentifier: String(describing: UITableViewCell.self), for: indexPath) as! UITableViewCell
cell.indexPath = indexPath
return cell

}

@IBAction func cellPlayButtonPressed(_ sender: UIButton) {

}
相关问题