Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
When working on UICollectionView, I am loading a cell from nib like below

Code:
(void)viewDidLoad
{
    [super viewDidLoad];
    /* Uncomment this block to use nib-based cells */
    UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];


    // Configure layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView.contentInset    =   UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 0.0f);

    [flowLayout setItemSize:CGSizeMake(300, 200)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:flowLayout];
}
And my nib file looks like the first image below

The top label is used to displayed the number of cell and the middle label is used to display a certain text

in method cellForItemAtIndexPath, I just want to set the text to the cell of certain row ( in this example, I am doing it at row of 1 ) :

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

     // Setup cell identifier
     static NSString *cellIdentifier = @"cvCell";

     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
     UILabel *titleLabel    = (UILabel *)[cell viewWithTag:100];
     UILabel *cellLablel    = (UILabel *)[cell viewWithTag:200];
     cellLablel.text         =   self.dataArray[0][indexPath.row];
     if ( indexPath.row == 1)
         [titleLabel setText:@"this is row 1"];

    return cell;

}
When running the app, there is a problem on it. Not only is titleLabel of cell at row1 set to This is row 1, but also the titleLabel of row5 and row9 and row10 is set as well. The second and third + fourth are illustration.
If anybody knows what I am doing wrong in the middle. please help.

My colleciton is containing 1 section and 15 row for this section.
 

Attachments

  • Screen Shot 2012-11-22 at 4.21.10 PM.png
    Screen Shot 2012-11-22 at 4.21.10 PM.png
    28.5 KB · Views: 211
  • Screen Shot 2012-11-22 at 4.29.37 PM.png
    Screen Shot 2012-11-22 at 4.29.37 PM.png
    37 KB · Views: 214
  • Screen Shot 2012-11-22 at 4.29.53 PM.png
    Screen Shot 2012-11-22 at 4.29.53 PM.png
    31.5 KB · Views: 143
  • Screen Shot 2012-11-22 at 4.30.02 PM.png
    Screen Shot 2012-11-22 at 4.30.02 PM.png
    32.7 KB · Views: 218

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
Could be quirk of how reusable cell works?

Your code is only setting the title label on cell1
So I'd expect that every third cell from there will have that label set with reuse.
What is curious is cell2 having the label set that way, it's like its a copy of cell1 instead of being created from the nib file.

Cell3 is cell0 which has come off screen to be reused and so on.
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
The expectation cellForRowAtIndex seems to be that you'll set all set able attributes of a cell before passing it off,as you never can be sure of previous state of the cell you receive.

You could turn your if into an if/else statement so indexes not passing the if test get the label set to a default value.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
The expectation cellForRowAtIndex seems to be that you'll set all set able attributes of a cell before passing it off,as you never can be sure of previous state of the cell you receive.

You could turn your if into an if/else statement so indexes not passing the if test get the label set to a default value.

Right. Collection views are like table views in that regard.

Any time you get a cell from the dequeue method, assume that you must fully configure it, including setting fields to default values. The cell might still have leftover information in it from the last time it was used.

In your case, you are recycling a row 1 cell for row 4, or 6, or 8, or whatever, and don't reset the title label to a default value. Thus, it still says, incorrectly, "this is row 1".

This drove me nuts when I first started using table views. I wasted DAYS trying to figure out why my table view cells were shoing incorrect values.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.