首页 > 解决方案 > 为什么 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)每次都添加了celldequeued 并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)

    }
}

再次感谢您阅读本文。

标签: iosswiftuitableviewdatasource

解决方案


试试这个为你的 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)

如果这不起作用,请告诉我


推荐阅读