2017-11-11 11 views
2

1つのTableViewCellをカラーにしたいが、最初は正しく動作していたが、別のランダムなセルも色づけしてしまう。Xcode UITableViewセルの背景が正しく動作しない

私はコードといくつかのスクリーンショットを残しています、あなたが助けることを願っています。私はXcode 9とスウィフト4を使用しています。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Linea", for: indexPath) 
    cell.textLabel?.text = ListaLineas[indexPath.row] 
    if (cell.textLabel?.text == "TifloInnova"){ 
     cell.textLabel?.textColor = UIColor(red: 100, green: 100, blue: 100, alpha: 1) 
     cell.contentView.backgroundColor = UIColor(red:100.0, green: 0.0, blue:0.0, alpha:0.8) 
    } 
    else{ 
     cell.textLabel?.textColor = UIColor(red: 0.0, green: 0.478, blue: 1, alpha: 1.0) 
    } 
    cell.textLabel?.numberOfLines = 6 
    cell.textLabel?.font.withSize(15) 
    cell.textLabel?.textAlignment = .center 
    cell.textLabel?.lineBreakMode = .byWordWrapping 

    tableView.estimatedRowHeight = 80 
    tableView.rowHeight = UITableViewAutomaticDimension 

    return cell; 
} 

多くのおかげです。 I want it to looks like this

But sometimes it looks like this

答えて

1

をあなたのケースのためのソリューションは、セルテキストラベルがされていない場合ことを確認しています"TifloInnova"をクリックして色をデフォルトに戻します。あなたは次のように実装することができます:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Linea", for: indexPath) 
     cell.textLabel?.text = ListaLineas[indexPath.row] 

     if (cell.textLabel?.text == "TifloInnova"){ 
      cell.textLabel?.textColor = UIColor(red: 100, green: 100, blue: 100, alpha: 1) 
      cell.contentView.backgroundColor = UIColor(red:100.0, green: 0.0, blue:0.0, alpha:0.8) 
     }else{ 
      cell.textLabel?.textColor = UIColor(red: 0.0, green: 0.478, blue: 1, alpha: 1.0) 
      // here what you should add, for instance the default color should be white: 
      cell.contentView.backgroundColor = UIColor.white 
     } 
     cell.textLabel?.numberOfLines = 6 
     cell.textLabel?.font.withSize(15) 
     cell.textLabel?.textAlignment = .center 
     cell.textLabel?.lineBreakMode = .byWordWrapping 

     tableView.estimatedRowHeight = 80 
     tableView.rowHeight = UITableViewAutomaticDimension 

     // btw no need for the ";" :) 
     return cell 
} 
+0

これは問題でした。私はその解決策については考えなかった。どうもありがとう! –

1

あなたはそうやって、「それ以外」の分岐に背景色をリセットすることがあります。

if cell.textLabel?.text == "TifloInnova" { 
    cell.contentView.backgroundColor = UIColor.red 
} else { 
    cell.contentView.backgroundColor = UIColor.clear  
} 
関連する問題