【发布时间】:2018-01-04 17:41:45
【问题描述】:
我有一个UITableViewCell,我在cellForRowIndexPath 方法中添加xib。在我更新模型并在UITableView 上调用 reloadData 之前,它工作正常。单元格在旧数据之上显示新数据,我可以在旧标签文本上看到标签。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let customView:CustomView = UIView.fromNib()
userView.frame = CGRect(x: 10, y: y, width: Int(self.tableView.bounds.size.width-50), height: 50)
userView.label.text = data[indexPath.row]
cell.addSubview(customView:)
你猜猜为什么会发生这种情况?
【问题讨论】:
-
如果单元格被重复使用,您需要在再次使用之前清除其内容
-
我添加了 let theSubviews: Array = (cell?.contentView.subviews)!在 theSubviews { view.removeFromSuperview() } 中查看,但子视图在 cellForRowIndexPath 中计数为 0
-
请使用
cellForRowIndexPath代码更新您的问题。 -
@PhillipMills 添加。谢谢
-
您正在将自定义视图添加为单元格的子视图,但是在您的评论中,您说您正在尝试将其从内容视图而不是单元格中删除。您可能希望将其添加到内容视图中。
标签: swift uitableview xib