iOS 表格 View 单元格随机为空

标签 ios swift uitableview

Screenshot of weird behavior

截图说明得很好。我有一个带有动态自定义单元格的表格 View 。我为单元格的内容之一添加了一个 println 来检查标签是否已设置。我可以在调试日志中看到每个单元格都有其内容。尽管如此,设备上仍然随机存在空单元格,这意味着没有内容出现的行正在发生很大变化。即使只是上下滚动也会使第二行消失,但第三行被填充。再次滚动会再次扭转这一局面。如果我关闭应用程序并再次启动它,每一行都会正确填充。

这是细胞生成的代码:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Return a count picker cell
    if countPickerTableRow == indexPath.row {

        ...
    }

    // Return a normal wish list entry cell
    else {

        let article = wishListEntries[indexPath.row]!
        let cell = tableView.dequeueReusableCellWithIdentifier("ArticleCell", forIndexPath: indexPath) as! WOSArticleCell

        // Correct the order in case a count picker cell was inserted
        var row = indexPath.row
        if countPickerTableRow != -1 && indexPath.row > countPickerTableRow {
            row--
        }

        cell.setThePreviewImage(UIImage(data: article.thumbnail))

        cell.setArticleName(article.name)
        cell.setArticleDescription(article.text)
        cell.setArticleNumber(article.number)
        cell.setArticleCount(article.count as Int)
        cell.setOrderInTable(row)
        cell.setTableViewController(self)

        cell.setNeedsDisplay()

        cell.setInputAccessoryView(numberToolbar) // do it for every relevant textfield if there are more than one

        println(String(indexPath.row) + " " + cell.nameLabel.text!)

        return cell
    }
}

在自定义单元类中没有什么特别的。唱片公司只有几个销售点。

这是 Storyboard的屏幕: Storyboard

谁能帮我看看这是怎么回事?我找不到调试日志可以输出单元格内容,但设备无法渲染它们的原因。

最佳答案

您应该更改代码的逻辑。如果 PickerCell 出现,只需调用 reloadData() 并重新加载 tableview 中的所有内容。如果您拥有的行数很小,这不会成为问题,并且这不是一个昂贵的操作,因为您在显示期间不会进行任何繁重的计算。

如果您由于在 PickerCell 中进行的更改而需要仅更新单个单元格,那么您应该使用要更新的单元格的索引路径调用 reloadRowsAtIndexPaths:withRowAnimation: 。

关于iOS 表格 View 单元格随机为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34204094/

相关文章:

ios - 过滤选项卡栏 Controller 中的选项卡?

IOS/ Storyboard/自动布局 : Center round button under horizontal bar

ios - 如何使用@available 定位低于特定操作系统的 iOS 设备

swift - 如何在 Swift 中找到字符串的所有大写组合?

ios - 如何自定义 UITableview 的右边框、上边框和下边框?

swift - 如何将 subview 添加到所有 UITable 单元格

ios - 在 Titanium Studio 中处理 iOS 的方向变化?

ios - 当 react native 应用程序 View 加载时,如何在 native ios 上调用 ViewController 类?

ios - 选择行时更改 UIImageView 颜色,如 UILabel 的突出显示属性?

ios - Objective-C - 核心数据关系的 JSON 格式,如 MySQL - 最佳实践