impo
impo

Reputation: 777

Swift 3 Custom Cell Changing on scroll

Sorry, I know this is a repeat question, but I have not been able to understand the answer from the other questions.

My custom cell is changing on scrolling. I know it is due to the reuse identifier, but I don't know how to fix it. Any help would be appreciated. I'm using indexPath.row to access an array as well.

How do I get my table view to not change values on scroll, even through I'm using a reuse identifier?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : TeamCell = tableView.dequeueReusableCell(withIdentifier: "TeamCell", for: indexPath) as! TeamCell

    cell.trainer.image = UIImage(named: "t\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].imageID)")
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p1.name != 0 {
        cell.pokemon1.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p1.name)")
        cell.pokemon1.isHidden = false
    } else {
        cell.pokemon1.isHidden = true
    }
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p2.name != 0 {
        cell.pokemon2.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p2.name)")
        cell.pokemon1.isHidden = false
    } else {
        cell.pokemon2.isHidden = true
    }
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p3.name != 0 {
        cell.pokemon3.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p3.name)")
        cell.pokemon1.isHidden = false
    } else {
        cell.pokemon3.isHidden = true
    }
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p4.name != 0 {
        cell.pokemon4.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p4.name)")
        cell.pokemon1.isHidden = false
    } else {
        cell.pokemon4.isHidden = true
    }
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p5.name != 0 {
        cell.pokemon5.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p5.name)")
        cell.pokemon1.isHidden = false
    } else {
        cell.pokemon5.isHidden = true
    }
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p6.name != 0 {
        cell.pokemon6.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p6.name)")
        cell.pokemon1.isHidden = false
    } else {
        cell.pokemon6.isHidden = true
    }

    return cell
}

(the function just checks if the value of an array is 0, if it isn't then show the image)

Upvotes: 0

Views: 971

Answers (2)

Björn Ro
Björn Ro

Reputation: 780

so like the comments says the problem was the wrong vissibly setting of pokemon1 in every if.

edit

well because of the downvote i will be more precice: In every if condition u set the pokemon1 to vissible. You need to use the Pokomen that your setting as image.

have fun coding

Upvotes: 1

Ketan Parmar
Ketan Parmar

Reputation: 27428

Set your every pokemon images to nil or some common placeholder image immediate after dequeue the cell, then put your every if-conditions and set every image view's default hidden property also, like if your imageview is hidden by default then set it's hidden property to true before setting images in if condition!

Upvotes: 1

Related Questions