ios - 未从 UITableViewCell 调用委托(delegate)方法

标签 ios swift uitableview

这是我第一次在 swift 中使用delegate。我有一个固定三行的表格 View ,每行都有一个单独的 .xib 单元格。

我正在根据单元格中获取的数据调整每个单元格的高度。因此,由于 tableView 是在之前加载的,单元格是在之后加载的,所以我在 UITableViewCell 类中定义了一个 protocol

上面的CalenderTimeTableViewCell类我已经:

protocol ReloadingTable {
    func updateTableView()
}

在上面的类中 awakeFromNib() 我已经: var myDelegate: ReloadingTable? 并在我正在调用的获取数据(使用 Alamofire)的方法中self.myDelegate?.updateTableView()

在具有 tableViewviewController 中,首先我扩展了类 ReloadingTable ,并且在这个类中我:

func updateTableView() {
        tableView.reloadData()
    }

通过应用断点,代码确实到达了 self.myDelegate?.updateTableView() 但在 viewController 中,updateTableView 方法没有被执行已调用。

最佳答案

正如 @vadian 所评论的,你应该将目标类分配给 myDelegate

在 cellForRowAt 函数中将目标类分配给我的委托(delegate)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CalenderTimeTableViewCell
    cell.myDelegate = self
    //Other
  }

关于ios - 未从 UITableViewCell 调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47305924/

相关文章:

ios - 在 UITableview 的 headerview 中将动态图像添加到 UIView

ios - UITableViewController 作为数据输入屏幕(iOS、Swift)和日历应用程序 "New Event"屏幕

ios - 有没有正确的方法来实现 TableView

ios - 将事件监听器添加到基于回合的 ios 游戏时发出警告

ios - 从 Firebase for Swift 中的查询检索数据

ios - UIPresentationController 崩溃只是指向 AppDelegate

iOS Swift - TableView 删除行错误

ios - insertRowsatIndexPath 异常 iOS

ios - 在 GameScene.swift 中显示 UIActivityViewController

iPhone:didSelectRowAtIndexPath 未调用