user2889249
user2889249

Reputation: 909

How to avoid UICollectionView repeating cells?

Hello I have a UICollectionView. I load data from a NSMutableArray and it has 4 objects. But my problem is its repeating the same cells again.

`

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return mutArrayArtists.count;
}



- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 2;
 }



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

UICollectionViewCell *cell = [collectionView   dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

artist = [mutArrayArtists objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = artist.artistImage;

UILabel *lblArtistname=(UILabel *)[cell viewWithTag:102];
lblArtistname.text=artist.artistName;

UILabel *lblSongCount=(UILabel *)[cell viewWithTag:101];
lblSongCount.text=artist.numberofSongs;
return cell;
 }

`

But the result is like this enter image description here

How can I avoid this? Please help me

Thanks

Upvotes: 0

Views: 1136

Answers (3)

H.P.
H.P.

Reputation: 113

This might be helpful

  - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

    {

        return 1;
    }

Upvotes: 1

Moataz Hossam
Moataz Hossam

Reputation: 413

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 1;  //instead of 2
}

The section here does not mean the number of columns. It means that the collection view will contain 2 sections vertically above each others

Upvotes: 2

button
button

Reputation: 656

This is the problem:

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 2;
}

You have two sections to the collection view, and in your cell provider, you don't distinguish the sections. Change it to say return 1; (assuming you do want just one section in the collection) or update the cellForItemAtIndexPath function (by inspecting indexPath.section) to split the sections out as you intend.

Upvotes: 1

Related Questions