can not set title for cell at specific indexPath.row of uicollectionview, UICollectio

Discussion in 'iOS Programming' started by tranvutuan, Nov 22, 2012.

  1. tranvutuan macrumors member

    Dec 19, 2011
    When working on UICollectionView, I am loading a cell from nib like below

        [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 ) :

    -(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.

    Attached Files:

  2. MattInOz macrumors 68030


    Jan 19, 2006
    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.
  3. tranvutuan thread starter macrumors member

    Dec 19, 2011
    it might be reuse issue now. Do you have any idead how to fix it ?
  4. MattInOz macrumors 68030


    Jan 19, 2006
    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.
  5. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    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.

Share This Page