我想使用协议将数据从 g3 = mkGraph (1,3) [(1,3,10), (2,3,1)]
视图控制器传递到不同视图控制器上的 UITableView。
首先,我创建了一个协议:
DetailedCellVC
然后,我在 protocol DetailedCellDelegate {
func getDetailedCellInfo(title: String)
}
中创建了一个委托变量:
DetailedCellVC
在 var cellDelegate: DetailedCellDelegate?
中,我创建了一个标签:
DetailedCellVC
在那之后,我在我的协议函数中使用了这个 let titleLabel: UILabel = {
let lbl = UILabel()
lbl.numberOfLines = 0
lbl.adjustsFontSizeToFitWidth = true
lbl.font = .boldSystemFont(ofSize: 26)
lbl.textAlignment = .center
return lbl
}()
:
titleLabel
然后,在另一个 ViewController @objc func didTapAddBarButton() {
guard let title = titleLabel.text else { return }
cellDelegate?.getDetailedCellInfo(title: title)
}
中,我添加了一行 FavouriteNewsVC
为了让它工作,我用我的协议创建了一个扩展:
detailedCellVC.cellDelegate = self
并使用 TableView 协议:
extension FavouriteNewsVC: DetailedCellDelegate {
func getDetailedCellInfo(title: String) {
titleLabel.text = title
}
}
问题是,当我点击 extension FavouriteNewsVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favouriteNews.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
if let title = favouriteNews[indexPath.row].title {
cell.titleLabel.text = title
}
return cell
}
}
中的“Add Bar Button”时,它不会将 DetailedCellVC
添加到 titleLabel
中的 tableView。我应该怎么做才能让它发挥作用?