swift - TableViewCell 作为代表没有收到任何东西
问题描述
我不明白为什么我的代表没有“收到”任何东西。稍后,我想通过使用委托将 ViewController 中的 UIImage 移交给 TableViewCell 内的 CollectionViewCell。ViewController 包含一个 tableView,我创建了一个带有 Nib (Xib) 文件的自定义单元格。每个单元格包含一个collectionView。tableview 的单元类是我的委托,但是当 ViewController 向委托“发送”(这是正确的术语吗?)任何东西时,什么都没有发生。这是我的视图控制器的代码:
protocol ViewControllerDelegate {
func delegateMethod(with string: String)
}
class ViewController: UIViewController {
var viewControllerdelegate: ViewControllerDelegate?
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet var suggestionTableView: UITableView!
var image = UIImage()
override func viewDidLoad() {
super.viewDidLoad() suggestionTableView.delegate = self
suggestionTableView.dataSource = self
suggestionTableView.register(UINib(nibName: "SuggestionTableViewCell", bundle: nil), forCellReuseIdentifier: "ID")
searchBar.delegate = self
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ID", for: indexPath) as! SuggestionTableViewCell
cell.currentImage = image
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
extension ViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print(searchText)
viewControllerdelegate?.delegateMethod(with: "Henlo")
}
}
这是我的 TableViewCell 类:
class SuggestionTableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var currentCollectionIndex = IndexPath()
var currentCell = UICollectionViewCell()
var currentImage = UIImage()
let controller = ViewController()
override func awakeFromNib() {
super.awakeFromNib()
controller.viewControllerdelegate = self
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension SuggestionTableViewCell: ViewControllerDelegate {
func delegateMethod(with string: String) {
print(string)
}
}
extension SuggestionTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionViewCell
cell.userImage.image = currentImage
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
为什么我的代表不工作?我很抱歉代码混乱,但是这次stackoverflow不允许我正确组织它......
解决方案
在 TableViewCell 类中,您创建 ViewController 类的实例
让控制器 = ViewController()
这将是与创建 TableViewCell 的实例不同的实例,因此当您设置错误实例的委托时,不会调用委托。
另外为什么你需要一个代表呢?我认为应该在 TableViewCell 类中创建一个函数。
推荐阅读
- python - 打开多个 CSV 文件并将其重塑为列
- android - 如何检索单个文档数据并在应用程序中显示?
- reactjs - 使用 Google Cloud Build 部署 Firebase 托管会引发“没有此类文件或目录”错误
- java - 创建一个虚拟的 java.net.http.HttpResponse 而不发出 HTTP 请求
- r - 如何使用 slice_min 遍历列
- regex - 正则表达式 - 从字符串中删除 html 标签,反引号之间的标签除外?
- javascript - 如何将另一个参数传递给 javascript map() 函数
- r - 使用 ggplot 的刻面线图
- node.js - 即使设置了 sameSite:'none' 和 secure: true 对于 MERN 堆栈 Web 应用程序,Cookie 也不会保存在 chrome 中
- laravel - 如何在laravel中使用外键获取数据并连接两个表