ios - UITableViewCell 仅添加到第 0 行时填充第 0、9、17、25 行中的数据

标签 ios swift uitableview

我在 tableView 中的数据有很大问题。 我使用代码在indexPath.row == 0中添加了图像:

 let business = self.results[indexPath.row]

 if indexPath.row == 0 {
        cell.timeImage.image = UIImage(named: "greenTime.png")
 }

indexPath.row 等于 0、9、17、25 ... 时显示我的图像

我的商家array.count = 40

谁能告诉我问题出在哪里吗?

编辑

当我移动表格高度时,我可以看到我的图像被添加到另一个indexPath.row。

最佳答案

正如 @Akhilrajtr 所写,您应该提供一个 else block 。原因是 iOS 重复使用单元格。如果您为一个单元格设置图像,该单元格会移出屏幕,因此会被重用,并且图像不会取消设置,即使您没有为此索引路径显式设置图像,它也会持续存在:

if indexPath.row == 0 {
    cell.timeImage.image = UIImage(named: "greenTime.png")
} else {
    cell.timeImage.image = nil
}

简而言之,每个 if block 都应该有一个 else block ,您可以在其中撤消在 if block 中所做的更改。

关于ios - UITableViewCell 仅添加到第 0 行时填充第 0、9、17、25 行中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699863/

相关文章:

ios - NSString stringByReplacingOccurrencesOfString 失败

ios - XCode: 'Warning: Multiple build commands for output file' 但如果我删除重复项,我会收到错误消息

ios - 添加 ViewController.swift 并重命名后,它不会将我的 viewController 与那个 swift 类链接起来

ios - 当我更改 iPhone 的语言时,会导致崩溃吗?

swift - 如何消除 UIStoryboard 中 TableView 单元格下方的边框?

ios - 在StoryTable之外的UITableViewController Xib中,静态单元格选项不可用

ios - 在谷歌地图中显示组标记 - Swift 3.1

ios - NSDate 在 Swift 中返回错误的日期

ios - 将 Podcast 提要添加到我的应用程序

ios - 全局保存和访问接收到的数据