Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.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
Ios 为UITableView单元格设置委托_Ios_Swift_Uitableview - Fatal编程技术网

Ios 为UITableView单元格设置委托

Ios 为UITableView单元格设置委托,ios,swift,uitableview,Ios,Swift,Uitableview,我的桌子视图有这个 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CurrentItemsCell { cell.isUserInteractio

我的桌子视图有这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CurrentItemsCell {
        cell.isUserInteractionEnabled = true
        let responseItems = currentItemsArray[indexPath.row]
        cell.configureCell(currentItem: responseItems)

        cell.setNeedsLayout()
        return cell
    } else {
        return CurrentItemsCell()
    }

}
这是我手机里的东西

class CurrentItemsCell: UITableViewCell {
    ...
    weak var mDelegate:MyProtocolImage?
    ...
}

@IBAction func showImages(_ sender: Any) {
    guard let indexPath = indexPath else { return }
    mDelegate?.sendIndexPathBack(row: indexPath.row)
    print("FROM THE CELL CLASS")
}

protocol MyProtocolImage: class {
    func sendIndexPathBack(row: Int)
}
在UITableView内部,我尝试添加cell.mDelagate=self,但出现了一个错误

Cannot assign value of type 'ItemResultsViewController' to type 'MyProtocolImage?'
如果我现在做cell.mDelegate=self-as!MyProtocolImage,语法很好,但应用程序崩溃

基本上,我在每个单元格内都有一个按钮,我需要获取单元格的indexRow,这样我就可以获取按下任何项目按钮的itemId。然后,我必须将itemId发送到一个新的ViewController(一个模态),并带有一个序列


任何帮助都会很棒

不要将
添加为!MyProtocolImage
。相反,声明您的类符合协议

class ItemResultsViewController: UITableViewController, MyProtocolImage {
然后将委托方法的实现添加到
ItemResultsViewController
类中:

func sendIndexPathBack(row: Int) {
    // do something useful
}
现在您可以执行以下操作:

cell.mDelagate = self
顺便说一句,您的单元格类中不应该有
indepath
属性。这可以利用更多的问题。您的委托应该将自身作为参数传递给委托方法。让委托方法的实现者根据需要确定单元的索引路径。永远不要告诉单元格其索引路径,因为随着时间的推移,它可能会出错

更新您的手机和协议:

@IBAction func showImages(_ sender: Any) {
    mDelegate?.sendIndexPathBack(cell: self)
    print("FROM THE CELL CLASS")
}

protocol MyProtocolImage: class {
    func sendIndexPathBack(cell: CurrentItemsCell)
}
然后在委托方法的实现中:

func sendIndexPathBack(cell: CurrentItemsCell) {
    if let indexPath = tableView.indexPath(for: cell) {
        // do something useful
    }
}

不要将
添加为!MyProtocolImage
。相反,声明您的类符合协议

class ItemResultsViewController: UITableViewController, MyProtocolImage {
然后将委托方法的实现添加到
ItemResultsViewController
类中:

func sendIndexPathBack(row: Int) {
    // do something useful
}
现在您可以执行以下操作:

cell.mDelagate = self
顺便说一句,您的单元格类中不应该有
indepath
属性。这可以利用更多的问题。您的委托应该将自身作为参数传递给委托方法。让委托方法的实现者根据需要确定单元的索引路径。永远不要告诉单元格其索引路径,因为随着时间的推移,它可能会出错

更新您的手机和协议:

@IBAction func showImages(_ sender: Any) {
    mDelegate?.sendIndexPathBack(cell: self)
    print("FROM THE CELL CLASS")
}

protocol MyProtocolImage: class {
    func sendIndexPathBack(cell: CurrentItemsCell)
}
然后在委托方法的实现中:

func sendIndexPathBack(cell: CurrentItemsCell) {
    if let indexPath = tableView.indexPath(for: cell) {
        // do something useful
    }
}

您可能还需要考虑命名约定。code>MyProtocolImage可能应该是
CurrentItemsCellDelegate
。而
sendIndexPathBack
应该反映实际发生的动作,比如
currentItemsCellShowImages(用于单元格:CurrentItemsCell
)`。是的,我通常将这些事情作为概念的证明,然后当我知道一切正常时,我会回去使用折射仪。非常感谢!您可能还需要考虑命名约定。code>MyProtocolImage可能应该是
CurrentItemsCellDelegate
。而
sendIndexPathBack
应该反映实际发生的动作,比如
currentItemsCellShowImages(用于单元格:CurrentItemsCell
)`。是的,我通常将这些事情作为概念的证明,然后当我知道一切正常时,我会回去使用折射仪。非常感谢!