Felipe Caldas Felipe Caldas - 7 months ago 26
Swift Question

TableView gets inconsistent behaviour when scrolling down/up

I have a ViewController that has a TableView. The tableView cell has a StackView with two images in it and outside it a label.

In the

tableView:cellForRowAtIndexPath
I get the cell and my data is inside a arrayList. And this is what I do:

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
}


So as you can see I hide the images depending on the gender of the name and also change the background of every other cell.

Now, the behavior I am seeing is:

https://gyazo.com/1b2d39696892b7fb2f15b71696d9a925

The gender is available for every object, I've checked.

What do you guys think? Thanks!

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