2014-10-16 72 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。一个用于第一个颜色的单元格,另一个用于其他颜色的单元格。