Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 22, 2012, 11:16 PM   #1
tranvutuan
macrumors member
 
Join Date: Dec 2011
can not set title for cell at specific indexPath.row of uicollectionview, UICollectio

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.
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-11-22 at 4.21.10 PM.png
Views:	38
Size:	28.5 KB
ID:	379191   Click image for larger version

Name:	Screen Shot 2012-11-22 at 4.29.37 PM.png
Views:	29
Size:	37.0 KB
ID:	379192   Click image for larger version

Name:	Screen Shot 2012-11-22 at 4.29.53 PM.png
Views:	29
Size:	31.5 KB
ID:	379193  

Click image for larger version

Name:	Screen Shot 2012-11-22 at 4.30.02 PM.png
Views:	29
Size:	32.7 KB
ID:	379194  
tranvutuan is offline   0 Reply With Quote
Old Nov 23, 2012, 02:53 AM   #2
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: 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.
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]
MattInOz is offline   0 Reply With Quote
Old Nov 23, 2012, 08:40 AM   #3
tranvutuan
Thread Starter
macrumors member
 
Join Date: Dec 2011
it might be reuse issue now. Do you have any idead how to fix it ?
tranvutuan is offline   0 Reply With Quote
Old Nov 23, 2012, 02:46 PM   #4
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: 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.
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]
MattInOz is offline   0 Reply With Quote
Old Nov 24, 2012, 08:03 PM   #5
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
see vendor information in user profile
Quote:
Originally Posted by MattInOz View Post
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.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 04:03 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC