我有一个像 [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/