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