ios - 向下/向上滚动时 TableView 出现不一致的行为

标签 ios swift uitableview

我有一个带有 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

我检查过,每个对象都有性别。

大家怎么看?谢谢!

最佳答案

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)
}

关于ios - 向下/向上滚动时 TableView 出现不一致的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084996/

相关文章:

ios - 创建新方案后找不到 Pod?

ios - 在 Swift 中隐藏 TabbedBarController 上的后退按钮

ios - 在同一个 UIViewController 中使用 NavigationDrawerController 和 ToolbarController

ios - 滚动 UITableView 与渐变困惑

ios - 在 iOS 9 中未调用 UITableViewCell 中的 IBAction

ios - 在 Swift 3 中从 URL 中提取 JSON 值返回 ":"表达式

ios - UISearchBar 提示出现在栏上,而不是在栏的顶部

ios - 为什么 Collection View 使用此代码加载中心轮播?

iphone - iOS 7 下 UITableView 部分索引相关的崩溃

ios - 通过 iOS 11 中的应用程序组共享用户默认值不起作用