Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

addArrangedSubview and UITAbleViewCell = infinite loop

I'm trying to create one cell via different XIB. So naturally, I tried that :

    let cell = tableView.dequeueReusableCellWithIdentifier("cellChapter", forIndexPath: indexPath) as! UniversalXIB

    if let subStackTitre = NSBundle.mainBundle().loadNibNamed("titreSection", owner: self, options: nil).first as? TitreXIB {
        cell.stackChapter.addArrangedSubview(subStackTitre)
        subStackTitre.titreOulet.text = "pouet nΒ°\(indexPath.row)"
    }

    if let subStackIcones = NSBundle.mainBundle().loadNibNamed("iconesStack", owner: self, options: nil).first as? IconesXIB {
        cell.stackChapter.addArrangedSubview(subStackIcones)
    }

    if let photoChapterImage = NSBundle.mainBundle().loadNibNamed("photoAvecLegende", owner: self, options: nil).first as? PhotoAvecLegendeXIB {
        cell.stackChapter.addArrangedSubview(photoChapterImage)
    }

However, every time I scroll, it adds the nib again, on top of the previous ones. I'm pretty sure the addArrangedSubview is guilty here, but I have absolutely no clue of another method… The trick is, I'm also going to add this :

    for i in texteArray {
        let titreBool = i["titre"] as! Bool
        let photoBool = i["photo"] as! Bool

    if titreBool == true && photoBool == false {

    if let ChapitreTitre = NSBundle.mainBundle().loadNibNamed("chapitreTitre", owner: self, options: nil).first as? ChapitreTitre {
        cell.stackChapter.addArrangedSubview(ChapitreTitre)

    }

        } else if titreBool == false && photoBool == false {

    if let ChapitreSansTitre = NSBundle.mainBundle().loadNibNamed("chapitreSansTitre", owner: self, options: nil).first as? ChapitreSansTitre {
        cell.stackChapter.addArrangedSubview(ChapitreSansTitre)

    }

        } else if titreBool == true && photoBool == true {

    if let ChapitreTitrePhoto = NSBundle.mainBundle().loadNibNamed("chapitreTitrePhoto", owner: self, options: nil).first as? ChapitreTitrePhoto {
                cell.stackChapter.addArrangedSubview(ChapitreTitrePhoto)

    }

        } else if titreBool == false && photoBool == true {

        if let ChapitreSansTitrePhoto = NSBundle.mainBundle().loadNibNamed("chapitreSansTitrePhoto", owner: self, options: nil).first as? ChapitreSansTitrePhoto {
            cell.stackChapter.addArrangedSubview(ChapitreSansTitrePhoto)

            }
        }
    }

So if I've got 5 elements in my array, it would have to create 5 elements based on two bools. If I've got 2 thousands, it would create 2 thousans chapters.

Any idea on how to proceed ? Or what am I doing wrong ?