Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用自定义UITableViewCell时检测UIButton_Uitableview_Swift_Uibutton_Custom Cell - Fatal编程技术网

使用自定义UITableViewCell时检测UIButton

使用自定义UITableViewCell时检测UIButton,uitableview,swift,uibutton,custom-cell,Uitableview,Swift,Uibutton,Custom Cell,我想实现一个自定义UITableViewCell类,它由标签、按钮和图像等子视图组成。这些单元格将显示使用API从web获取的内容 我不想实现UITableView委托方法didSelectRowAtIndexPath,因为这将使整个单元格可选择。只有单元格中的按钮才能触发任何操作 该按钮通过IBOutlet从情节提要连接到自定义UITableViewCell。在UITableViewController类的CellForRowatingIndexPath中的UIButton上调用addTarg

我想实现一个自定义UITableViewCell类,它由标签、按钮和图像等子视图组成。这些单元格将显示使用API从web获取的内容

我不想实现UITableView委托方法
didSelectRowAtIndexPath
,因为这将使整个单元格可选择。只有单元格中的按钮才能触发任何操作

该按钮通过IBOutlet从情节提要连接到自定义UITableViewCell。在UITableViewController类的
CellForRowatingIndexPath
中的UIButton上调用
addTarget(uU2;:操作:forControlEvents:)
方法

当我们想要在按钮的选择器功能中检测所选单元格的
indepath
时,就会出现障碍

这就是如何在选择器函数中检测选定单元格的
indexPath

@IBAction func doSomething(sender: AnyObject) {
    var location: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(location)!
    println("The indexPath for Selected Cell is - \(indexPath.row)")
}
虽然,这成功地让我绕过了这个问题,但我的问题是

1) 您是否找到了使用UIButtons在自定义UITableViewCell中传递选定单元格数据的替代方法


2) 到目前为止,在Swift中实现类似场景的最佳实践是什么?

一种方法是迭代发送方的超级视图,看看是否可以找到UITableViewCell

假设您使用一个方法创建了一个通用UIView扩展,该方法将检查其superview是否为UITableViewCell

extension UIView {
    func parentTableViewCell() -> UITableViewCell? {
        var view = self
        while let superview = view.superview {
            if let cell = superview as? UITableViewCell {
                return cell
            } else {
                view = superview
            }
        }
        return nil
    }
}
然后你可以做以下事情

if let cell = (sender as UIView).parentTableViewCell() {
    let indexPath = tableView.indexPathForCell(cell)
    println("The row for this cell is - \(indexPath.row)")
}
另一种方法是使用视图的
tag
属性,将其设置为Int,然后检查方法中发送者的
标记是什么

i、 e.在
表视图中:cellforrowatinexpath:
方法

cell.myButton.tag = indexPath.row
let rowIndex = (sender as UIButton).tag
println("The row for this cell is - \(rowIndex)"
然后在你的
doSomething
方法中

cell.myButton.tag = indexPath.row
let rowIndex = (sender as UIButton).tag
println("The row for this cell is - \(rowIndex)"

如果tableView只有一个部分,则可以使用按钮的
标记
属性来存储
indexPath.row

cellforrowatinexpath
中,为按钮设置目标操作时,设置
button.tag=indexPath.row

然后在您的
剂量测定程序中:

@IBAction doSomething(sender: UIButton) {
    println("This button is from row - \(sender.tag)")
}

顺便说一句,您的发件人属于
AnyObject
类型,因此调用
convertPoint:toView
而不将其强制转换为UIView将导致编译错误。我同意。我想我也会遇到编译错误,但是上面提到的
@IBAction func
没有任何这样的错误。尽管如此,将“发送者”类型设置为UIView
将是一种相对干净的方法。可能它没有给出编译错误,因为发送方作为AnyObject完全有可能实现convertPoint:toView方法。这种方法也很有效。比我的方法简洁得多。