【问题标题】:How to set data from ViewController to UITableView custom Cell如何将数据从 ViewController 设置到 UITableView 自定义 Cell
【发布时间】:2016-09-29 09:26:56
【问题描述】:

我一直在尝试使用自定义委托将数据从 Viewcontroller 传递到 UITableView 自定义单元格。有将数据从自定义单元格传递到 ViewController 的答案,但反之则没有。谁能建议我一些想法或示例代码。

【问题讨论】:

  • 你能展示你尝试过的代码吗
  • 你不能直接通过。但是你可以通过使用模型类来做到这一点。模型类包含您要传递的数据。在viewcontroller 中,您可以将数据保存到模型。在 customcell 类中,您可以进行检索。
  • 为什么要否决这个问题?
  • 你所说的模型类是什么意思。你指的是共享类吗? @user3774439
  • 是的。你可以使用它

标签: ios objective-c delegates custom-cell


【解决方案1】:

我不确定是不是你的意思,但是你可以在function tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell中设置数据

例如:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: ViewController.cellIdentifier, for: indexPath) as! CustomCell
        cell.data = data 
        return cell

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2016-05-23
    相关资源
    最近更新 更多