【问题标题】:TableView gets inconsistent behaviour when scrolling down/up向下/向上滚动时 TableView 的行为不一致
【发布时间】:2016-05-07 05:47:31
【问题描述】:

我有一个带有 TableView 的 ViewController。 tableView 单元格有一个 StackView,里面有两张图片,外面有一个标签。

tableView:cellForRowAtIndexPath 中,我得到了单元格,我的数据在arrayList 中。这就是我所做的:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "NamesTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! NamesTableViewCell

    let _name = _Names[indexPath.row]

    if indexPath.row % 2 != 0 {
        cell.backgroundColor = UIColor(hexString: "ededed")
    }

    cell.labelName.text = _name.name

    if _name.gender! == "M" {
        cell.pinkCircleImageView.hidden = true
    } else if _name.gender! == "F" {
        cell.blueCircleImageView.hidden = true
    }

    return cell
}

如您所见,我会根据名称的性别隐藏图像,并更改每个其他单元格的背景。

现在,我看到的行为是:

https://gyazo.com/1b2d39696892b7fb2f15b71696d9a925

我检查了每个对象的性别。

你们怎么看?谢谢!

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:
    cell.pinkCircleImageView.hidden = false
    cell.blueCircleImageView.hidden = false
    
    if _name.gender! == "M" {
        cell.pinkCircleImageView.hidden = true
        cell.bringSubviewToFront(blueCircleImageView)
    } else if _name.gender! == "F" {
        cell.blueCircleImageView.hidden = true
        cell.bringSubviewToFront(pinkCircleImageView)
    }
    
    if indexPath.row % 2 == 0
    {
        cell.backgroundColor=UIColor.whiteColor()
    }
    else
    {
        cell.backgroundColor=UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
    }
    

    【讨论】:

    • 嗨,杰伊什。非常感谢,它完全有效!不过,我必须说我不明白为什么它会起作用。 bringSubviewToFront() 是什么意思?此外,还有最后一个问题。单元格背景颜色正在丢失。它应该是白色的,然后是灰色的。但是当我向下滚动时,一切都变成灰色。知道为什么吗?再次感谢
    • bringSubviewToFront() 表示移动指定的子视图,使其出现在其兄弟视图之上。
    • 单元格背景颜色应该是白色然后是灰色...尝试编辑答案
    • 就是这样!现在一切正常。问题是我没有将单元格设置回白色,所以在滚动时它保持灰色。谢谢你!
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    相关资源
    最近更新 更多