简体   繁体   中英

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. but unable to compare with StoredNames array.

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) . Keep in mind that else statement is necessary because of reusable functionality . Also variable names should be 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 :

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

Please use this in cellforrow at indepath

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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