2016-05-27 10 views
0

Ich habe eine UITableViewCell Unterklasse, die eine Bool enthält. Diese Eigenschaft sollte beibehalten werden (während die Zelle sichtbar ist), jedoch scheint es so eingerichtet zu sein, dass, wenn collectionView:cellForItemAtIndexPath: aufgerufen wird, die Eigenschaft zurückgesetzt wird. BeispielPersist UITableViewCell Unterklasse Eigenschaft

class CellClass: UITableViewCell { 

    var someBool: Bool = false 

    func doSomethingWithBool() { 
    if someBool { 
     thing1() 
     someBool = false 
    } else { 
     thing2() 
     someBool = true 
    } 
    } 
} 



class CollectionView: UICollectionView { 

    @IBAction func buttonPressed() { 
    // Do some things 

    // cellForItemAtIndexPath called again here 
    self.collectionView?.reloadItemsAtIndexPaths(paths) 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CellClass 

    // Everything else 

    if collectionViewProperty { 
     // This doesn't work right because someBool is reset to false 
     cell.doSomething() 
    } 

    return cell 
} 

Muß ich someBool in einer init Methode setzen müssen? Was ist der richtige Weg dies zu gestalten?

+0

Hat die doSomething() Funktion ändern someBool außer Kraft setzen? – sebenalern

+0

Ja, es ändert einigeBool – Ja5onHoffman

Antwort

1

würde ich prepareForReuse in Ihrer benutzerdefinierten Zelle

override func prepareForReuse() { 
    super.prepareForReuse() 
    someBool = false 
} 
Verwandte Themen