【问题标题】:UIButton color in UIScrollView is set to wrong color when scrolling滚动时 UIScrollView 中的 UIButton 颜色设置为错误颜色
【发布时间】:2014-10-16 15:10:45
【问题描述】:

我有一个 20 行的 UITableView 并且在每一行中我有 1 个 UIImageView、4 个 UITextView 和 3 个 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 中。那怎么了?

【问题讨论】:

  • @FawadMasud 一个布尔值数组,如果推文已被转发,则为真。
  • 我建议在自定义单元格中创建布尔属性并像 cell.retweetedA 一样访问它们。问题似乎与滚动时的单元格索引有关。

标签: ios objective-c uitableview swift uibutton


【解决方案1】:

使用另一个Identifier,TableView不会重绘所有单元格,你必须声明两个CellIdentifier。一个用于具有第一种颜色的单元格,另一个用于另一种颜色的单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2011-06-13
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多