Mhmd Backer Shehadi
Mhmd Backer Shehadi

Reputation: 589

UICollectionViewCell reuse

There is any code like this one that work with UICollectionViewCell when the method reuse ?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

    NSString *CellId = [NSString stringWithFormat:@"CellId%d%d",indexPath.row,indexPath.section];

    if (!cell) 
    {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId] autorelease];
    }

    return cell;
}

Upvotes: 1

Views: 4132

Answers (3)

dariaa
dariaa

Reputation: 6385

The whole point is to reuse cells, that's why the reuse identifier should be the same for all cells, at least all cells of one class (that's why it makes sense to declare CellId as a static variable - this method will be called a lot). dequeueReusableCellWithReuseIdentifier: method returns cell ready to be reused, if any. If there is no such cell you should create it and later, when it is no longer visible UICollectoinView will add it into "reusable cells pool" and return for dequeueReusableCellWithReuseIdentifier:.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath    {

    static NSString *CellId = @"YourCellIdentifier";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellId];
    if (!cell) {
        cell = [[CustomCell alloc] initWithFrame:yourFrame];
    }

    return cell;
}

Upvotes: 1

Marius Waldal
Marius Waldal

Reputation: 9952

Yes, there is:

UICollectionReusableView *collView = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

You can find the info in the docs: https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/dequeueReusableCellWithReuseIdentifier:forIndexPath:

Upvotes: 0

PunjabiCoder
PunjabiCoder

Reputation: 525

Yes, there is a similar method for collectionview as shown below:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"collectionCell";

    collectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}

Upvotes: 0

Related Questions