Reputation: 12514
I am having a very peculiar problem with a VC that contains a UICollectionView in it. I am trying to have cells that have been previously selected by the user, 1) Reduce its alpha, and 2) Unhide an icon at the top left of the cell. My collection loads twice. Once with the data source previously stored in the device, and then once fresh data is loaded from an API.
What I find is that at first the view loads correctly, but then, strangely, the alpha color is correct in every cell, but some cells unhide the icon on the second load!
I have no idea what could be causing this. Any help?
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
if defaults.boolForKey("gotPL") {
let catNumber = cell.viewWithTag(1) as! UILabel
let catTitle = cell.viewWithTag(2) as! UILabel
catNumber.text = String(indexPath.item + 1)
catTitle.text = defaults.arrayForKey("playlistTitles")![indexPath.item] as? String
cell.layer.cornerRadius = 30
//CUSTOM LOAD FUNCTIONALITY STARTS HERE
if defaults.arrayForKey("selectedArray") != nil {
let selectedArray: [String] = defaults.arrayForKey("selectedArray") as! [String]
if selectedArray.contains(defaults.arrayForKey("playlistTitles")![indexPath.item] as! String) {
print(indexPath.item)
cell.alpha = 0.5 //WORKS CORRECTLY
cell.viewWithTag(3)?.hidden = false //IS REPLICATED INCORRECTLY ON SECOND LOAD
}
}
return cell
}
return cell
}
Upvotes: 0
Views: 89
Reputation: 330
You need refresh value of cell.alpha
and cell.alpha
for every cell rather than if
condition cell condition. The reason is cell reuse.
cell.alpha = 1
cell.viewWithTag(3)?.hidden = true
if defaults.arrayForKey("selectedArray") != nil {
let selectedArray: [String] = defaults.arrayForKey("selectedArray") as! [String]
if selectedArray.contains(defaults.arrayForKey("playlistTitles")![indexPath.item] as! String) {
print(indexPath.item)
cell.alpha = 0.5 //WORKS CORRECTLY
cell.viewWithTag(3)?.hidden = false //IS REPLICATED INCORRECTLY ON SECOND LOAD
}
}
good luck!
Upvotes: 2