Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 23, 2012, 12:16 AM   #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:	55
Size:	28.5 KB
ID:	379191   Click image for larger version

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

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

Click image for larger version

Name:	Screen Shot 2012-11-22 at 4.30.02 PM.png
Views:	53
Size:	32.7 KB
ID:	379194  
tranvutuan is offline   0 Reply With Quote
Old Nov 23, 2012, 03: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, 09: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, 03: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, 09:03 PM   #5
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
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

Similar Threads
thread Thread Starter Forum Replies Last Post
When searching in uitableview, the indexpath of the selected cell is always null zacattac50 iPhone/iPad Programming 1 Apr 5, 2014 01:59 PM
Set label title from mouseEntered: on a NSButton Val K Mac Programming 1 Jun 19, 2013 04:39 AM
UICollectionView show strange when cell has background colro mikezang iPhone/iPad Programming 3 Nov 8, 2012 10:09 AM
How can I set “numbers” to return a specific integer in a set? rotarypower101 Mac Applications and Mac App Store 0 Sep 3, 2012 03:30 PM
Difference between a cell and a row in Tableviews? animefx iPhone/iPad Programming 4 Aug 7, 2012 03:35 PM

Forum Jump

All times are GMT -5. The time now is 01:19 PM.

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

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