Felipe Caldas
Felipe Caldas

Reputation: 2503

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!

Upvotes: 0

Views: 87

Answers (1)

Jayesh Miruliya
Jayesh Miruliya

Reputation: 3317

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

Upvotes: 2

Related Questions