UIScrollView中的UIButton颜色在滚动时设置为错误的颜色



我有一个有20行的UITableView,每行有1个UIImageView、4个UITextView和3个UIButton。

我的表格视图加载完美,但当我滚动时,一些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中。那怎么了?

使用另一个标识符,TableView不会重新绘制所有单元格,您必须声明两个CellIdentifier。一个用于具有第一颜色的单元格,另一个用于另一颜色的单元格。

最新更新