2014-10-16 67 views
0

我有一個UITableView,有20行並且在每一行中我都有1個UIImageView,4個UITextView和3個UIButton。UIScrollView中的UIButton顏色在滾動時設置爲錯誤的顏色

我的tablview加載完美,但是當我滾動時,一些UIBUtton顯示錯誤的顏色。例如 只有一行應該有黃色的UIColor,但是當我滾動另一行時的另一個解除按鈕變成黃色!

這裏是我的代碼:

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

    var cell:TimelineTableViewCell? = tableView.dequeueReusableCellWithIdentifier("TimelineTableViewCell") as? TimelineTableViewCell 

    if (cell == nil){ 
     cell = NSBundle.mainBundle().loadNibNamed("TimelineTableViewCell", owner: self, options: nil)[0] as? TimelineTableViewCell 
    } 

    cell!.nameLabel.text = self.userNameA[indexPath.row] 
    cell!.screenNameLabel.text = "@" + self.screenNameA[indexPath.row] 
    cell!.tweetLabel.text = self.tweetA[indexPath.row] 

    cell?.avatarImage.hnk_setImageFromURL(NSURL(string:self.avatarA[indexPath.row])) 
    cell?.avatarImage.layer.cornerRadius = 4 
    cell?.avatarImage.layer.masksToBounds = true 

     if langA[indexPath.row] == "fa" { 
      cell?.tweetLabel.textAlignment = NSTextAlignment.Right 
     } else if langA[indexPath.row] == "ar" { 
      cell?.tweetLabel.textAlignment = NSTextAlignment.Right 
     } 

     cell?.favButton.setTitle(favorite_countA[indexPath.row], forState: .Normal) 
     cell?.reTweetButton.setTitle(retweet_countA[indexPath.row], forState: .Normal) 

     if favoritedA[indexPath.row] { 
      cell?.favButton.setTitleColor("#FFE066".UIColor, forState: .Normal) 
     } 

     if retweetedA[indexPath.row] { 
      cell?.reTweetButton.setTitleColor("#70B894".UIColor, forState: .Normal) 
     } 


    cell?.reTweetButton.tag = indexPath.row; 
    cell?.reTweetButton.addTarget(self, action: "retweetPressed:", forControlEvents: UIControlEvents.TouchUpInside) 

    cell?.favButton.tag = indexPath.row; 
    cell?.favButton.addTarget(self, action: "favPressed:", forControlEvents: UIControlEvents.TouchUpInside) 

    return cell; 
} 

它只出現在的UIButton。那麼,怎麼了?

+0

什麼是retweetedA? – 2014-10-16 15:20:12

+0

@FawadMasud Bool值的數組,如果推文已被轉發,則返回true。 – 2014-10-16 15:32:51

+0

我會建議在自定義單元格中創建bool屬性並像cell.retweetedA一樣訪問它們。這個問題似乎與滾動時單元格的索引有關。 – 2014-10-16 15:51:24

回答

0

使用另一個標識符,TableView不重繪所有單元格,你必須聲明兩個CellIdentifier。一個用於第一個顏色的單元格,另一個用於其他顏色的單元格。