Scrolling issue with UICollectionView.

Discussion in 'iOS Programming' started by zaxonus, Aug 20, 2013.

  1. zaxonus macrumors newbie

    Joined:
    Dec 23, 2010
    #1
    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?
     
  2. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #2
    Your not posting the code.... let's start there.
     
  3. zaxonus thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    #3
    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).
     
  4. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #4
    I'm pretty sure you have some funny business going on in your cellForItemAtIndexPath.
     
  5. zaxonus thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    #5
    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;
    }
    
     
  6. ChristianJapan macrumors 68040

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #6
    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 ...
     
  7. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #7
    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.
     
  8. zaxonus thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    #8
    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.
     
  9. ChristianJapan macrumors 68040

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #9
    How is your cell defined ? Storyboard vs programmatically ? Is the outlet correct connected ? use of AutoLayout ? Can you share more code; e.g. adjustTextLabel
     
  10. zaxonus thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    #10
    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;
            }
        }
    }
    
     
  11. zaxonus thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    #11
    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.
     
  12. ChristianJapan macrumors 68040

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #12
    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 ?
     

Share This Page