首页 > 解决方案 > 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不允许我正确组织它......

标签: swiftdelegates

解决方案


在 TableViewCell 类中,您创建 ViewController 类的实例

让控制器 = ViewController()

这将是与创建 TableViewCell 的实例不同的实例,因此当您设置错误实例的委托时,不会调用委托。

另外为什么你需要一个代表呢?我认为应该在 TableViewCell 类中创建一个函数。


推荐阅读