2
votes

I have an issue to add extra cell to my collection view I have change a logic to use data model to fill my cells

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures.count + 1
    }


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

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("images", forIndexPath: indexPath) as! ProfileImageCell

        let picture = pictures[indexPath.item] as! Gallery

        print(indexPath.item)

        if indexPath.item == pictures.count {
            cell.image.image = UIImage(named: "ProfilePlusImage")
        } else {
            cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))
            cell.imageId = picture.id.integerValue
        }

        return cell
    }

The issue is in this line I think

let picture = pictures[indexPath.item] as! Gallery

When I mark it and mark

cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))

It adding extra cell on 11-th place. But other way gives my beyond bounds

Could anyone help me?

2
Yes, you should verify that indexPath.item is less than pictures.count before you use it as an index into pictures. You seem to have the answer.i_am_jorf
@i_am_jorf no still index 10 beyond bounds [0 .. 9]Anton
Simply move that line inside the else statementPaulw11
@Paulw11 thank it's working, I spent 2 hours to understand what broken :)Anton

2 Answers

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("images", forIndexPath: indexPath) as! ProfileImageCell

    print(indexPath.item)

    if indexPath.item == pictures.count {
        cell.image.image = UIImage(named: "ProfilePlusImage")
    } else {
        let picture = pictures[indexPath.item] as! Gallery

        cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))
        cell.imageId = picture.id.integerValue
    }

    return cell
}
0
votes

It seems that your CollectionView have one section so this code should work

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0 {
        return 1
    }
    return pictures.count
}

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("images", forIndexPath: indexPath) as! ProfileImageCell

    let picture = pictures[indexPath.item] as! Gallery

    print(indexPath.item)


    if indexPath.section == 0 {
        cell.image.image = UIImage(named: "ProfilePlusImage")
    } else {
        cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))
        cell.imageId = picture.id.integerValue
    }

    return cell
}