gpt4 book ai didi

ios - 如何将数据从 VC 传递到 Cell?

转载 作者:行者123 更新时间:2023-11-28 10:13:28 24 4
gpt4 key购买 nike

我想知道如何将数据从 ViewController 传递到单元格?

我是初学者,所以我可能会监督显而易见的 :P

我有一个家庭 VC,当您按下 commentButton 时,您将进入包含帖子 postId 的 CommentVC。因为我希望能够喜欢一条评论(效果很好)并注意到用户他的评论被喜欢(现在不起作用)所以我不仅需要在 commentVC 中拥有 postId(其中包含正确的评论) ) 也在单元格中。

这是我将数据从 HomeVc 传递到 CommentVC 的代码

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "CommentSegue" {
let commentVC = segue.destination as! CommentViewController
let commentCell = CommentTableViewCell()
let postId = sender as! String
commentCell.postId = postId
commentVC.postId = postId
}
}

当我在 CommentVc 和 CommentCell 中打印出两个变量时,只有 CommentVc 显示正确的变量,而 Cell 的打印输出语句为“nil”。

知道如何通过它吗?

最佳答案

您不应该通过调用您的自定义类初始化器来实例化 UITableViewCell,但您应该在您的 UITableViewController 类(或符合 UITableViewContollerDataSource 协议(protocol))。

在使用 创建单元格时,将要在单元格中显示的数据传递给 TableView Controller 和数据源方法(例如 tableView(_:cellForRowAt:)) dequeueReusableCell(withIdentifier:) 将数据分配给特定的单元格。

关于ios - 如何将数据从 VC 传递到 Cell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44785132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com