我有两个数组
names = ["Gopal", "Harish", "Kartik","Raj", "Vishal", "Manoj", "James"]
StoredNames = ["kartik", "Vishal", "James"]
我正在将名称数组显示到表格视图中。但我的任务是,如果名称数组包含 StoredNames 数组元素,我需要更改特定的单元格颜色。谁能指导我完成这项任务。我使用的是代码。但无法与StoredNames数组进行比较。
override func tableView(_ tableView: UITableView, willDisplay cell:UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = .yellow
if let index = names.index(of: "kartik") {
cell.backgroundColor = indexPath.row == index ? .green : .white
}
return cell
}
编辑后:
func filterRowsForSearchedText(_ searchText: String) {
filteredModels = models.filter({( model : Contact) -> Bool in
return model.name.lowercased().contains(searchText.lowercased())||model.number.lowercased().contains(searchText.lowercased())
})
contactsTableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = contactsTableView.dequeueReusableCell(withIdentifier: "ContactsTableViewCell", for: indexPath) as! ContactsTableViewCell
let model: Contact
if searchController.isActive && searchController.searchBar.text != "" {
model = filteredModels[indexPath.row]
} else {
model = models[indexPath.row]
}
cell.nameLabel.text = model.name
cell.numberLabel.text = model.number
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if storedNumberArr.contains(numberArr[indexPath.row]) {
//Name is there, color it
cell.backgroundColor = .green
} else {
// Color for non existance.
cell.backgroundColor = .white
}
}