Как изменить определенный цвет ячейки в ios swift3 - PullRequest
0 голосов
/ 16 ноября 2018

У меня есть массив строк, например [1,2,3,4,5]. Я отображаю этот массив в табличном виде. Мой вопрос: если я хочу изменить цвет только 3-й ячейки, как я могу сравнить 3-й элемент с массивом в ios swift?

Ответы [ 3 ]

0 голосов
/ 16 ноября 2018

Вы также можете проверить условие внутри функции cellforrow:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCellIndentifier", for: indexPath) as! ExampleCell

        if indexPath.row == 2{
            // Do your modification on the 3rd cell item
            cell.backgroundColor = .red
        }

        return cell
    } 
0 голосов
/ 16 ноября 2018

Я только что применил условие под cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldInTableViewCell") as! myProfieTabelViewCellTableViewCell
    if indexPath[1] % 2  == 0{
         cell.backgroundColor = #colorLiteral(red: 0.9088078997, green: 0.9088078997, blue: 0.9088078997, alpha: 1)
        print(indexPath[1])
    }
    else{
         cell.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    }
    cell.selectionStyle = .none
    return cell
}
0 голосов
/ 16 ноября 2018

в методе делегата табличного представления в зависимости от условия измените цвет на нужный вам

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 условия на основе, используйте как

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 
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...