I downloaded a collection of images and loaded them into a collectionView. I want to be able to add the individual item selected to an array I declared globally whenever I press on an individual cell so then I can loop through them to be deleted from core data later. This line prints fine in terms of the order of the item - print("You selected cell #(indexPath.item)!"), but the 2nd time I press another cell to add to the array, I get an fatal error: Index out of range error. I don't know what I'm getting this.
var selectedCell: [Int] = [] -> Declared globally
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// handle tap events
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
print("You selected cell #\(indexPath.item)!")
if self.selectedCell.contains(indexPath.item){
print("Item already added")
} else {
self.selectedCell.append(indexPath.item)
}
if selectedCell.count > 0 {
toolbarButton.title = "Remove Item"
}
// let selectCell:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
// selectCell.contentView.backgroundColor = UIColor.whiteColor()
}