简体   繁体   English

如何在iOS swift4中更改特定的UITableviewcell颜色

[英]How to change particular UITableviewcell colour in iOS swift4

I have two arrays like 我有两个数组

names  = ["Gopal", "Harish", "Kartik","Raj", "Vishal", "Manoj", "James"] 
StoredNames = ["kartik", "Vishal", "James"]

I am displaying names Array into tableview. 我在表视图中显示名称数组。 But My Task is if names array contains StoredNames array elements, I need to change that particular cell colour.Could anyone guide me to do this task.I am using is code. 但是我的任务是如果名称数组包含StoredNames数组元素,我需要更改特定的单元格颜色。任何人都可以指导我执行此任务。我正在使用的是代码。 but unable to compare with StoredNames array. 但无法与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
}

After edit: 编辑后:

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
    }
}

You can use contains(element) . 您可以使用contains(element) Keep in mind that else statement is necessary because of reusable functionality . 请记住,由于reusable functionality ,else语句是必需的。 Also variable names should be camelCase . 变量名称也应该是camelCase

let names  = ["Gopal","Harish","Kartik","Raj","Vishal","Manoj","James"]
let storedNames = ["kartik","Vishal","James"]

if storedNames.contains(names[indexPath.row]) {
    //Name is there, color it
    cell.backgroundColor = .green 
} else {
    // Color for non existance.
    cell.backgroundColor = .white 
}

As suggested by dahiya_boy, if you want to compare string discarding their cases you can replace above if-condition by this : 正如dahiya_boy建议,如果要比较两个字符串丢弃他们的cases ,你可以在上面替换if-condition本:

if storedNames.map({ $0.lowercased()}).contains(names[indexPath.row].lowercased()) {

Please use this in cellforrow at indepath 请在indepath的cellforrow中使用它

if(indexPath.row % 2 == 0) {
    cell.backgroundColor = UIColor.init(rgb: 0xebebeb)
} else{
    cell.backgroundColor = UIColor.init(rgb: 0xdcdcdc)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM