如何解决TableViewCell 作为委托没有收到任何东西
我不明白为什么我的代表没有“收到”任何东西。稍后,我想通过使用委托将 ViewController 中的 UIImage 移交给 TableViewCell 内的 CollectionViewCell。 ViewController 包含一个 tableView,我使用 Nib (Xib) 文件创建了一个自定义单元格。每个单元格都包含一个 collectionView。 tableview 的单元格类是我的委托,但是当 ViewController 向委托“发送”(这是正确的术语吗?)任何东西时,什么也没有发生。这是我的 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",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 类中创建一个函数应该可以。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。