Eli
Eli

Reputation: 768

UICollectionView setting first cell to always be specific content

Hi so I'm using a side scrolling UICollectionView to display groups of people that the user makes. The groups are stored on my server and when the view loads, they load from the server. However I want the first cell to always be the same which is a cell which lets you create groups. This is the layout i need.

enter image description here

I know how to use multiple different custom cells, but how do I make it so the first cell is static and the cells after load content from my servers? Thanks :)

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

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

    if indexPath.row == 0 {
    let cell : AddGroupCollectionViewCell = collectionViewOutlet.dequeueReusableCellWithReuseIdentifier("Add", forIndexPath: indexPath) as! AddGroupCollectionViewCell

    return cell

    } else {

    let cell : FriendGroupsCell = collectionViewOutlet.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FriendGroupsCell


    cell.groupImage.image = UIImage(named: "pp")
    cell.groupNameLabel.text = familyName[indexPath.row]

    return cell
    }
}

This is my code and it misses out the first person in the array because the index path skips over it. How can i modify this so it works

Upvotes: 2

Views: 4339

Answers (1)

Wingzero
Wingzero

Reputation: 9754

UICollectionViewCell is leveraging reusing techniques to improve performance. Remember this. Nothing can be static in a cell, because this cell later will be on another index.

You can use collectionView:cellForItemAtIndexPath: to make the first cell always load the same images/labels via indexPath.row == 0

You can use prepareReuse method to clean up the resources in the cell. So if cell No.2 is going to be the new No.1 cell, it get a chance to clean up old resources.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : AddGroupCollectionViewCell = collectionViewOutlet.dequeueReusableCellWithReuseIdentifier("Add", forIndexPath: indexPath) as! AddGroupCollectionViewCell
    if indexPath.row == 0 {
        cell.groupImage.image = UIImage(named: "new")
        cell.groupNameLabel.text = "new"
    } else {
        cell.groupImage.image = UIImage(named: "pp")
        cell.groupNameLabel.text = familyName[indexPath.row]
    }
    return cell
}

Upvotes: 3

Related Questions