【发布时间】:2021-04-29 19:57:36
【问题描述】:
我的代码很简单。我有一个要在 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”。每次重启模拟器都略有不同。
正如您在下面的代码中看到的,我每次将cell 出列并设置cell.note 时都添加了print(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)
}
}
再次感谢您阅读本文。
【问题讨论】:
标签: ios swift uitableview datasource