当我在TableView中向下滚动时,单元格数据会发生变化--Swift 4

时间:2018-06-16 04:26:39

标签: swift tableview

当我在表格视图中向下滚动时,已消失的单元格数据会发生变化。我该如何解决这个问题?

import

2 个答案:

答案 0 :(得分:1)

每次表格视图请求单元格时,您都会更新number。这与正在显示的行没有直接关系。

目前还不清楚你为何拥有number财产。

如果您只想在每个单元格中显示相应的行号,请删除number属性并更新:

cell.textLabel?.text = "\(number)"

使用:

cell.textLabel?.text = "\(indexPath.row)"

答案 1 :(得分:0)

不清楚问题,但我认为你想在tableview重新加载时滚动到底部吗?

extension UITableView {
    func scrollToBottom(animated: Bool = false) {
        let section = self.numberOfSections
        if (section > 0) {
            let row = self.numberOfRows(inSection: section - 1)
            if (row > 0) {
                 self.scrollToRow(at: IndexPath(row: row - 1, section: section - 1), at: .bottom, animated: animated)
            }
        }
    }
}

通话时,您需要使用“async”

DispatchQueue.main.async(execute: {
    yourTableView.reloadData()
    yourTableView.scrollToBottom()
}
祝你好运。