ios - 为什么 UITableVIew 滚动后显示数据不正确
问题描述
我的代码非常简单。我有一个要在 UITableView 上显示的固定字符串列表,带有自定义 UITableViewCell,它上面只有一个 UILabel。
我的问题是,虽然数据最初显示正确,但滚动后显示不正确。
这里有更多细节。DataSource 是一个列表[(String, something_else)]
列表的内容是
[("aaaaaa", something),
("bbbbbb", something),
("cccccc", something),
...
("rrrrrr", something)]
屏幕足够大,可以显示“aaaa”到“mmmm”。当我向下滚动时,下一个可见行应该是“nnnn”,但它是“rrrr”,在“rrrr”之后是“aaaa”、“bbbb”、“cccc”,然后我向上滚动,它给了我“eeee” ,“cccc”,“rrrr”。每次我重新启动模拟器时都会略有不同。
正如您在下面的代码中看到的那样,我print(cell.note)
每次都添加了cell
dequeued 并cell.note
已设置,但控制台中的打印消息表明单元格已设置为正确的注释。是的,它看起来是正确的。在“mmm”之后,它是 n、o、p、q、r,然后我向上滚动,它是 f、e、d、c、b、a,它看起来是正确的。
我不明白为什么,也不知道如何解决。谷歌搜索没有提供任何帮助,所以我想尝试在这里寻求帮助。谢谢您阅读此篇。
下面是我的代码。非常简单的dataSource和delegate实现
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
data.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview?.dequeueReusableCell(withIdentifier: "NoteNodeCell") as! NoteNodeCell
cell.note = data[indexPath.row].0
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64
}
}
NoteNodeCell 是一个自定义的 UITableViewCell。我基本上只在其中添加了一个 UILabel 。
class NoteNodeCell: UITableViewCell {
var note: String {
get { cellView.note }
set { cellView.note = newValue }}
var cellView:CellView = CellView()
override func awakeFromNib() {
super.awakeFromNib()
if cellView.superview != contentView {
contentView.addSubview(cellView)
}
cellView.translatesAutoresizingMaskIntoConstraints = false
cellView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
cellView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
cellView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
}
override func prepareForReuse() {
super.prepareForReuse()
note = ""
}
}
class CellView: UIView {
var note = "" { didSet { print(note) }}
override func draw(_ rect: CGRect) {
super.draw(rect)
UIColor.white.setFill()
UIRectFill(rect)
let label = UILabel(frame: bounds)
label.text = " " + note
label.font = UIFont(name: "Courier", size: 18)
label.textColor = .white
label.backgroundColor = .gray
label.layer.cornerRadius = 5
label.layer.masksToBounds = true
UIColor.clear.setFill()
addSubview(label)
}
}
再次感谢您阅读本文。
解决方案
试试这个为你的 CellView 课程
class CellView: UIView {
let label = UILabel()
var note = "" {
didSet {
print(note)
self.label.text = " " + note // this
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
private func setupSubviews() {
label.text = " " + note
label.font = UIFont(name: "Courier", size: 18)
label.textColor = .white
label.backgroundColor = .gray
label.layer.cornerRadius = 5
label.layer.masksToBounds = true
addSubview(label)
}
}
你的 UITableViewCell 需要这个
let cellView = CellView(frame: .zero)
如果这不起作用,请告诉我
推荐阅读
- eclipse-plugin - 如何在我的 RCP 应用程序中集成 Eclipse 中可用的第三方插件?
- excel - 比较两列(材料和尺寸)并从 EXCEL 中的另一列(报价金额)获取输出
- javascript - 解析带有“&”字符的 URL 参数,后面没有“=”
- c++ - 地图清除抛出异常
- php - 如何使用 PHP 获取仅包含特殊查询字符串的 URL?
- java - 将 youtube 上的视频从 android 应用上传到特定帐户的最佳方式是什么?
- javascript - 如何正确格式化和发送应用程序/json-patch+json
- styles - 在 Openlayers 3 中选择要素(点、线、多边形)时自定义顶点样式?
- xml - 使用 PowerShell 读取 XML 文件并显示值
- excel - VBA:“自动化错误。发生异常。” 使用用户窗体时