Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 如何将回调从TableView传递到TableView单元格?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何将回调从TableView传递到TableView单元格?

Ios 如何将回调从TableView传递到TableView单元格?,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个tableview。在tableview的每个单元格中,都有一个上载按钮。单击该按钮,我使用协议委托机制告诉TableViewVC我需要显示文件选择器选项。我不明白的是,当为特定单元格选择特定文件时,如何将回调从TableView传递到TableView单元格,以便更改该单元格中标签的可见性 单击单元格中的按钮时,下面是我的代码 protocol FileUploadDelegate { func uploadFile(documentId: Int,position: Int)

我有一个
tableview
。在
tableview
的每个单元格中,都有一个上载按钮。单击该按钮,我使用协议委托机制告诉TableViewVC我需要显示文件选择器选项。我不明白的是,当为特定单元格选择特定文件时,如何将回调从TableView传递到TableView单元格,以便更改该单元格中标签的可见性

单击单元格中的按钮时,下面是我的代码

protocol FileUploadDelegate {
    func uploadFile(documentId: Int,position: Int)
}

@IBAction func uploadDocumentClicked(_ sender: Any) {
        delegate?.uploadFile(documentId: documentId,position: position)
    }
在我的
TableViewVC
中,我实现了协议并编写了文件选择器的所有代码

现在使用下面的方法

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

}

我需要将回调从tableView传递到触发该操作的确切tableCell,以便更改该单元格中标签的可见性。我如何才能做到这一点?

您可以这样修改此方法:

protocol FileUploadDelegate {
    func uploadFile(documentId: Int,position: Int, cellIndexPath: IndexPath)
}
您将获得稍后需要更新的内容。您可以另存为属性(例如) 在此之后,您可以在方法中更新:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

}
通过
tableView.reloadData()

cellforrowatinexpath
中,您可以使用保存的indexpath更新特殊单元格

TableViewCell.swift

class tableViewCell: UITableViewCell{
 var onButtonTapped : (() -> Void)? = nil
 override func awakeFromNib() {
    super.awakeFromNib()

 }
 @IBAction func buttonTapped(_ sender: Any) {
    if let onButtonTapped = self.onButtonTapped {
        onButtonTapped()
    }
}
}
ViewController.swift

class ViewController: UIViewController, UICollectionViewDelegate, 
UICollectionViewDataSource{

  func collectionView(_ collectionView: UICollectionView, 
  numberOfItemsInSection 
  section: Int) -> Int {
     guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: 
     "youridhere", for: indexPath) as? cellname else 
     { return UICollectionViewCell() }

        cell.onButtonTapped = {
            filepicker.item = . 
 self.model.item[indexPath.row]
        }
        return cell

  }
}

点击ui按钮时,它将上载文件或显示文档选择器?@wint它将显示文档picker@Kutta你有单元格的数据模型吗?@subramanianamariappan No