小编Hee*_*low的帖子

Swift 3 - 在多个自定义单元格中重用单元格的问题

在 UITableview 中向下滚动时遇到问题。当单元格被重用时,表格向我显示了具有旧内容的单元格。

问题如下:

Swift 想要重用旧单元格,但没有正确清除旧单元格中的旧内容。这会导致单元格具有旧内容,尽管我正在为单元格提供新数据。

UITableView 的架构,如果如下:

  • 每个自定义单元格都有自己的标识符
  • 每个自定义单元格都在一个自己的类中分开

问题截图:

问卷开始截图

截屏

向下滚动的表格

截屏

这里的问题是“Handedness”-Cell,它显示了单元格编号 3(因为单元格的重用),这是不对的

numberOfSection-方法

override func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}
Run Code Online (Sandbox Code Playgroud)

numberOfRowsInSection-方法

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(section == 0){
        return questionnaireStructure.count
    } else {
        return 1
    }
}
Run Code Online (Sandbox Code Playgroud)

cellForRowAt 方法

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // first section is the normal Questionnaire
    if(indexPath.section == 0){

        // current questionnaireStructure
        let …
Run Code Online (Sandbox Code Playgroud)

uitableview custom-cell ios swift

2
推荐指数
1
解决办法
3538
查看次数

标签 统计

custom-cell ×1

ios ×1

swift ×1

uitableview ×1