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

zaxonus

macrumors newbie
Original poster
Dec 23, 2010
22
0
I am having a hard time trying to figure out what is wrong using UICollectionView and UICollectionViewLayout.
I use UICollectionView and UICollectionViewLayout smoothly, but when I scroll things start to get strange.
Only the initially displayed cells can be seen, and none of the others.
I have verified that collectionView: cellForItemAtIndexPath: gets called and has the adequate data.
But I never see the cells appear on the display.
What could I be doing wrong?
 
I would be happy to post some code if I had an idea where the problem was.
I am starting using UICollectionView and there are a number of places where things could be wrong I suppose. If someone with experience using UICollectionView gives me a hint on a possible source of problems then I will be able to look closer and post code (unless the hint is enough for me to solve the issue on my own).
 
I'm pretty sure you have some funny business going on in your cellForItemAtIndexPath.
 
Well, here is my code; let me know if you can spot something:

Code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChapterViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.textContent.text=[sentenceArray objectAtIndex:indexPath.row];
    [cell adjustTextLabel:[self.delegate chapterFontSize]];

    if (![sentenceSizeDico valueForKey:[NSString stringWithFormat:@"%d",indexPath.row]]) {
        [sentenceSizeDico setValue:[NSValue valueWithCGSize:cell.textContent.frame.size]
                            forKey:[NSString stringWithFormat:@"%d",indexPath.row]];
    }

    return cell;
}
 
Don't you have to use .item instead of .row from IndexPath in case of CollectionView ?
Maybe have a look if .raw contain the expected values for your array access ...
 
So what's happening? Only the first screen worth of cells can be seen?

If you don't adjust the label and do the other stuff, do the cells appear?

Like already mentioned you probably shouldn't be using indexpath.row.
 
Thanks for your reply. If I change .row to .item it behaves exactly the same way, no better no worse.
I need to do the adjust label and the other stuff, otherwise the cells do not appear.
sentenceSizeDico is a dictionnary where I store the cells size for future display needs.
With some NSLog tracing I can see that collectionView:cellForItemAtIndexPath: is called for the missing cells when I scroll, but nothing gets displayed on the device or simulator screen.
 
How is your cell defined ? Storyboard vs programmatically ? Is the outlet correct connected ? use of AutoLayout ? Can you share more code; e.g. adjustTextLabel
 
The cell is defined in Storyboard and I do not use AutoLayout. Here the subclass of UICollectionViewCell that I use:


Code:
//  ChapterViewCell.h file.

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ChapterViewCell : UICollectionViewCell

@property (nonatomic,retain) IBOutlet UILabel *textContent;

- (void)adjustTextLabel:(CGFloat)size;

@end

And inside the file ChapterViewCell.m here is the code for adjustTextLabel:

Code:
- (void)adjustTextLabel:(CGFloat)size
{
    self.backgroundColor=[UIColor clearColor];
    textContent.backgroundColor=[[UIColor darkGrayColor] colorWithAlphaComponent:0.6];
    textContent.textColor=[UIColor whiteColor];
    textContent.font=[textContent.font fontWithSize:size];
    textContent.layer.cornerRadius=13.0;
    textContent.layer.borderColor=[UIColor yellowColor].CGColor;
    textContent.layer.borderWidth=2.0;
    CGSize referenceSize,challengeSize;
    CGRect varFrame;
    textContent.frame=CGRectMake(0.0,0.0,280.0,36.0);
    referenceSize=CGSizeMake([UIScreen mainScreen].bounds.size.width,
                             [UIScreen mainScreen].bounds.size.height);
    challengeSize=[textContent.text sizeWithFont:textContent.font constrainedToSize:referenceSize];

    if (((challengeSize.width<=textContent.frame.size.width)&&(challengeSize.height<=textContent.frame.size.height))&&
        ((challengeSize.width!=textContent.frame.size.width)||(challengeSize.height!=textContent.frame.size.height))) {
        varFrame=textContent.frame;
        varFrame.size=CGSizeMake(challengeSize.width+10.0,challengeSize.height+6.0);
        textContent.frame=varFrame;
    } else {
        if (challengeSize.height>textContent.frame.size.height) {
            varFrame=textContent.frame;
            varFrame.size=CGSizeMake(challengeSize.width+10.0,challengeSize.height+6.0);
            textContent.frame=varFrame;
        }
    }
}
 
Even if I reduce the collectionView:cellForItemAtIndexPath: method to the following, I still have the same issue of cells not showing up as I scroll.

Code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChapterViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.textContent.text=[NSString stringWithFormat:@"%d",indexPath.item];
    cell.textContent.frame=CGRectMake(0.0,0.0,40.0,50.0);
    if (![sentenceSizeDico valueForKey:[NSString stringWithFormat:@"%d",indexPath.item]]) {
        [sentenceSizeDico setValue:[NSValue valueWithCGSize:CGSizeMake(40.0,50.0)]
                            forKey:[NSString stringWithFormat:@"%d",indexPath.item]];
    }
    return cell;
}
I must be making a stupid mistake somewhere. Everything I can think about I have checked, but I can't get rid of this problem.
 
Can you also share code form where you define number of segments and number of item; size of cells etc.
also seems you are not using ARC ?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.