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