ios - 如何更改 ios swift3 中的特定单元格颜色

标签 ios iphone swift

我有一个像 [1,2,3,4,5] 这样的字符串数组。我在 tableview 中显示这个数组。我的问题是,如果我只想更改第 3 个单元格的颜色,如何在 ios swift 中比较第 3 个元素和数组?

最佳答案

根据条件在您的 tableview 委托(delegate)方法上更改您想要的颜色

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = indexPath.row == 2 ? .red : yellow //or use cell.contentView.backgroundColor = = indexPath.row == 2 ? .red : yellow
}

如果你想改变包含 3 个条件的数组,那么使用 like

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = yourArrayName[indexPath.row] == "3" ? .red : yellow //or use cell.contentView.backgroundColor = = yourArrayName[indexPath.row] == "3" ? .red : yellow
}

好吧,我们换一种方式

 override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
   cell.backgroundColor = .yellow
  if let index = yourArrayName.index(of: "3") {
   cell.backgroundColor = indexPath.row == index ? .red : yellow 
  }
}

关于ios - 如何更改 ios swift3 中的特定单元格颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53333448/

相关文章:

ios - Swift - 编译器错误 - 段错误 11 - 泛型符合协议(protocol)

json - 如何从可选的 AnyObjects 创建 SwiftyJSON 字典?

iphone - 图标已经包含光泽效果不适用于 ios 5

ios - 如何以编程方式确定 iOS 中的默认电子邮件帐户/地址?

ios - UIPresentationController - 演示 View 大小?

iphone - 系统声音 ID 音量

ios - 以编程方式更改为从右到左的 RTL

javascript - iphone/ipad 杂志框架、iScroll 或更新版本?

iphone - InAPP 购买的内容存储在哪里?

Objective-C 转换为 Swift 3.1 出现错误 'Any' 不可转换