更改特定表视图单元格的颜色而不更改其他

时间:2019-08-28 14:58:41

标签: swift

主要问题是,当我尝试向pdf图标之类的单元格中添加图像或根据单元格值更改单元格文本的颜色时,它会更改我不打算更改的单元格。同样,在模拟器之间看起来也有所不同。例如,XSmax可以正常工作不是因为我的漏洞修复程序,而是xs都被我不想更改的多个单元弄乱了。

我已经尝试调试代码,甚至试图通过重置受影响的单元格来以非常棘手的方式对其进行修复。

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell: UITableViewCell
       let fieldCellIdentifier = "FieldCell"
       if let fieldCell = tableView.dequeueReusableCell(withIdentifier: fieldCellIdentifier) {
           cell = fieldCell
       }
       else {
           cell = UITableViewCell(style: .value1, reuseIdentifier: fieldCellIdentifier)
       }
       let row = wellInfoSections[indexPath.section].rows[indexPath.row]
       cell.textLabel?.text = row.label
       cell.detailTextLabel?.text = row.value
       if indexPath.section == WellSummarySections.contacts.rawValue || indexPath.section == WellSummarySections.pdf.rawValue {
           cell.accessoryType = .disclosureIndicator
       }
       if indexPath.section == WellSummarySections.pdf.rawValue {
           cell.imageView?.tintColor = PelotonGoStyle.Palette.darkRed
           cell.imageView?.image = #imageLiteral(resourceName: "pdf")
       }

//I just expect that only the cells I want to change to change not the cells that I do not want. Kinda vague but so is the issue.

1 个答案:

答案 0 :(得分:0)

您可以在UITableView单元格上使用prepareForReuse()方法,并将颜色设置回默认值:

https://developer.apple.com/documentation/uikit/uitableviewcell/1623223-prepareforreuse