当我点击我的按钮时,我尝试在单元格中获取 indexPath.row:
@IBAction func cellPlayButtonPressed(_ sender: Any) {
let cell = (sender as AnyObject).superview??.superview as? UITableViewCell
let indexPath = runnerTableView.indexPath(for: cell!)
print(indexPath)
}
当我按下按钮时出现此错误:
致命错误:在解开 Optional 值时意外发现 nil
我已经尝试了在 Stack Overflow 上找到的所有内容,但没有任何效果。
让我知道错误在哪里!谢谢 :)
只需复制给定的扩展名
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)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句