2016-09-25 14 views
0

私は14種類のビューを表示しているカスタムセルを持っています。セルが受信しているデータに応じて、いくつかのデータの数に等しいビューが表示され、どのデータであるかによって色が変わる必要があります。例えばUITableViewからの表示が消えています

それは3つのタイプのデータを受信して​​いる場合は、それが唯一の3つのビューを表示する必要があります。 ice cream, candy, marshmellowsとすることができます。次に、それはorange (ice cream), blue (candy), green (marshmellows)に3つのビューを表示します。

私はそれをうまく機能させました。私はそれに興奮しています。問題は、3つのビューを表示しているセルを1つのビューのみを含むセルにスクロールすると、最初に3つのビューを表示する最初のセルまでスクロールすると、それは唯一の私は例を持っている...

を1、最初のものを示す:ストーリーボードで

マイカスタムセルは、このようなものです、緑と黒のボックスは異なるビュー enter image description here

あるこれは何のことです6種類のデータのようです: enter image description here

私はその後、1つのビューを含むセルまでスクロールすると、6つのビューのセルは、その後、このようになります。

私はコードを説明してみましょう:

enter image description here

をここにいくつかの関連するコードです。私のデータベースでは、すべての投稿には1または2のカテゴリがあります。これは、コードが探しているものです(update.category)。カテゴリ1の場合は、単なるテキストです。それがカテゴリ2(または何か他のもの)の場合は、ビューを表示する必要があります。実際には、私のUITableViewController内のセルのタイプにする必要があります。

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

    let update = updates[indexPath.row] 

    if update.category == 1 { 

     let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell 

     cell.nameButton.setTitle(update.addedByUser, forState: .Normal) 

     return cell 

    } else { 

     let cellSugar:newSugarTableViewCell = tableView.dequeueReusableCellWithIdentifier("CellSugar", forIndexPath: indexPath) as! newSugarTableViewCell 

     cellSugar.nameButton.setTitle(update.addedByUser, forState: .Normal) 

     let sugarArray = update.content.componentsSeparatedByString("--") 

     dispatch_async(dispatch_get_main_queue(), {() -> Void in 

      cellSugar.sugarViewOne.layer.cornerRadius = cellSugar.sugarViewOne.frame.size.width/2 
      cellSugar.sugarViewOne.clipsToBounds = true 
      cellSugar.sugarViewOne.layer.borderColor = UIColor.whiteColor().CGColor 
      cellSugar.sugarViewOne.layer.borderWidth = 2.0 

      cellSugar.sugarViewTwo.layer.cornerRadius = cellSugar.sugarViewTwo.frame.size.width/2 
      cellSugar.sugarViewTwo.clipsToBounds = true 
      cellSugar.sugarViewTwo.layer.borderColor = UIColor.whiteColor().CGColor 
      cellSugar.sugarViewTwo.layer.borderWidth = 2.0 
     }) 


     if sugarArray.count == 1 { 

      dispatch_async(dispatch_get_main_queue(), {() -> Void in 
       let seperateSugarArray = sugarArray[0].componentsSeparatedByString("#") 

       if seperateSugarArray[4] == "Candy" { 
        cellSugar.sugarViewOne.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå 

       } else if seperateSugarArray[4] == "Ice cream" { 
        cellSugar.sugarViewOne.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå 

       } else if seperateSugarArray[4] == "Marshmellows" { 
        cellSugar.sugarViewOne.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis 

       } 

       cellSugar.sugarViewTwo.hidden = true 
       cellSugar.sugarViewThree.hidden = true 
       cellSugar.sugarViewFour.hidden = true 
       cellSugar.sugarViewFive.hidden = true 
       cellSugar.sugarViewSix.hidden = true 
       cellSugar.sugarViewSeven.hidden = true 
       cellSugar.sugarViewEight.hidden = true 
       cellSugar.sugarViewNine.hidden = true 
       cellSugar.sugarViewTen.hidden = true 
       cellSugar.sugarViewEleven.hidden = true 
       cellSugar.sugarViewTwelve.hidden = true 
       cellSugar.sugarViewThirteen.hidden = true 
       cellSugar.sugarViewFourteen.hidden = true 
      }) 


     } else if sugarArray.count == 2 { 
      dispatch_async(dispatch_get_main_queue(), {() -> Void in 

       let seperateSugarArray = sugarArray[0].componentsSeparatedByString("#") 
       let seperateSugarArrayTwo = sugarArray[1].componentsSeparatedByString("#") 

       if seperateSugarArray[4] == "Candy" { 
        cellSugar.sugarViewOne.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå 

       } else if seperateSugarArray[4] == "Ice cream" { 
        cellSugar.sugarViewOne.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå 

       } else if seperateSugarArray[4] == "Marshmellows" { 
        cellSugar.sugarViewOne.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis 

       } 

       if seperateSugarArray[4] == "Candy" { 
        cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå 

       } else if seperateSugarArray[4] == "Ice cream" { 
        cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå 

       } else if seperateSugarArray[4] == "Marshmellows" { 
        cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis 

       } 


       cellSugar.sugarViewThree.hidden = true 
       cellSugar.sugarViewFour.hidden = true 
       cellSugar.sugarViewFive.hidden = true 
       cellSugar.sugarViewSix.hidden = true 
       cellSugar.sugarViewSeven.hidden = true 
       cellSugar.sugarViewEight.hidden = true 
       cellSugar.sugarViewNine.hidden = true 
       cellSugar.sugarViewTen.hidden = true 
       cellSugar.sugarViewEleven.hidden = true 
       cellSugar.sugarViewTwelve.hidden = true 
       cellSugar.sugarViewThirteen.hidden = true 
       cellSugar.sugarViewFourteen.hidden = true 


      }) 

     } 

     return cellSugar 
    } 
} 

私はあなたが理解し、それはあなたがそれぞれの条件のすべての要素を更新する必要が

+0

@ Mr.UB私は関連するコードを追加しました。ビューを含んでいる部分はelse部分にあり、if update.category == 1ではなく、それはテキストを持つセルだけです。 –

+0

私はそれを追加しました:-) –

+0

もし 'update.category == 1 {'の 'else'部分が' return cell'を含んでいません。 –

答えて

1

:-)かなり迷惑であるとして、あなたが、私を助けることができることを願っています。 問題の原因:テーブルビューではセルが再利用されているため、セルに5つのラベルが表示され、スクロールした後に別のセルに3があり、もう1つが非表示になっている場合は、もう一度表示されると、テーブルビューは2つの隠れたラベルを持つビューを使用しています。 CellForRowAtIndexPathには、 の各条件またはカスタムUItableViewCellクラスに不足しているラベルを追加すると、prepareForReuseというメソッドが追加され、すべてのラベルが表示されます。

+0

2つのビューだけで私に例を教えてください。各条件で不足しているビューを追加しますか?あなたはprepareForReuseをどのように使っていますか? –

+0

他の部分の上部にあるすべてのビューを追加し、それらを.hidden = falseに設定しました。シンプルだけど複雑すぎる - ありがとうございます:-) –

0

UITableViewは、現在の状態(すべてのビューが設定されている状態)で1つのセルを取得し、必要に応じてセルを再構成する必要がある別の行のセルを返します。

if update.category == 1 { 

    let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell 
    cell.nameButton.setTitle(update.addedByUser, forState: .Normal) 
    return cell 

} 

現在の行データを返す前にセルを設定する必要があります。

+0

これは、ビューを表示しているコードの一部ではありません。 –

関連する問題