开发者社区> 问答> 正文

如何更新UITableViewCell?

我有自定义的带有属性的UITableViewCellcount: Int?...根据这个属性,我呈现UITextField的数量。(一.G.count == 1->一个UITextField,count == 2->两个UITextField)。

稍后,当某些事件发生时,我希望为该标签设置新值。在这里,我所做的:

在我的UIViewController中:

@objc func onDidReceiveEvent(_ notification: Notification) {
        guard let count = notification.object as? Int else { return }
        guard let cell = self.tableView.cellForRow(at: IndexPath(row: 2, section: 0)) as? MyCustomCell else { return }

        cell.count = count
        self.tableView.reloadRows(at: [IndexPath(row: 2, section: 0)], with: .automatic)
        // I also tried self.tableView.reloadData()
}

我把断点放在override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)我看到这个单元格是在这个代码块之后更新的,但是count没有更新。我做错什么了?

展开
收起
游客5akardh5cojhg 2019-12-25 16:17:10 1934 0
1 条回答
写回答
取消 提交回答
  • 在控制器中创建一个属性count

    var count = 0 // or another default value 在……里面cellForRow赋值count到count细胞的性质

    if indexPath.row == 2 { cell.count = count } 在……里面onDidReceiveEvent更新count并重新加载行

    @objc func onDidReceiveEvent(_ notification: Notification) { guard let count = notification.object as? Int else { return } self.count = count self.tableView.reloadRows(at: [IndexPath(row: 2, section: 0)], with: .automatic) }

    2019-12-25 16:17:26
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载