5

色付きのテーブル ビュー セルを使用して他のセルをカテゴリに分けるアプリを作成しています。if else ステートメントを使用して、セルをさまざまな色に着色することでこれを行っています。しかし、何らかの理由で、アプリを起動すると、テーブル ビューを上下にスクロールするほど、他のセルもランダムに色が変わります。これは、カスタムの instrumentTableCell クラスのコードです。

@IBOutlet var nameLabel: UILabel?
@IBOutlet var descriptionLabel: UILabel?
@IBOutlet var thumbnailImage: UIImageView!

func configurateTheCell(recipie: Recipie) {
    self.nameLabel?.text = recipie.name
    self.descriptionLabel?.text = recipie.description
    self.thumbnailImage?.image = UIImage(named: recipie.thumbnails)
    if nameLabel!.text == "Instrument"
    {
        backgroundColor = UIColor.orangeColor()
        nameLabel?.textColor = UIColor.blackColor()
    }
    else if nameLabel!.text == "Optional addon"
    {
        backgroundColor = UIColor.cyanColor()
    }

}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if nameLabel!.text == "Instrument"
    {
        return 20
    }
    else if nameLabel!.text == "Optional addon"
    {
        return 20
    }
    else
    {
        return 100
    }


}

アプリを起動すると、次のようになります。 アプリ起動時

vs. ユーザーが少しスクロールしたとき: スクロールした後

また、誰かがその方法を知っていれば、色付きのセルも小さくして、アプリの見栄えを良くしたいと思います。

4

1 に答える 1

2

cellForRowAtIndexPathデリゲートメソッドで設定できます

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("your identifier", forIndexPath: indexPath)
     if cell.recipie.name == "Instrument"
     {
      backgroundColor = UIColor.orangeColor()
      nameLabel?.textColor = UIColor.blackColor()
     }
    else if cell.recipie.name == "Optional addon"
     {
      backgroundColor = UIColor.cyanColor()
     }
   else{
    backgroundColor = UIColor.whiteColor()
   }
  return cell
 }
于 2016-07-20T13:31:38.710 に答える