batistomorrow
batistomorrow

Reputation: 674

Trouble with UICollectionView : does not display labels of all of its cells

I have created a UICollectionView programmatically. I use a custom UICollectionViewCell subclass. Inside the cell class, I create a label with a class of my own (easier and quicker to set up its appearance). The problem I'm having is : for several cells, the collectionView does not layout the label content. I know that the data is here (print in the console) that is, the text property of the cell does contain the string data I want to show, but for some reason the collectionView does not display the label content. I tried with a simple test (print 'toto' inside the label) and I get a few toto's here and there, but not in all of the cells. As you can see I have 2 UICollectionViews inside the same ViewController, and this is why I test whether it's one or the other in the DataSource implementation.

Please tell me if you need more code.

Here's the code :

-(void)createBottomCollectionView {

    // Layout
    UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];

    collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    collectionViewLayout.minimumLineSpacing = 0.0;

    // UICollectionView
    self.bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 354+20, 320-2*20, 35) collectionViewLayout:collectionViewLayout];

    self.bottomCollectionView.showsHorizontalScrollIndicator = NO;
    self.bottomCollectionView.bounces = YES;
    self.bottomCollectionView.alwaysBounceHorizontal = YES;
    self.bottomCollectionView.alwaysBounceVertical = NO;
    self.bottomCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.bottomCollectionView.dataSource = self;
    self.bottomCollectionView.delegate = self;

    [self.bottomCollectionView registerClass:[SetFormatCollectionViewCell class] forCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier];

    // Background
    self.bottomCollectionView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:self.bottomCollectionView];

    [self.bottomCollectionView reloadData];
}

CollectionView datasource (dumb test with a "toto" value) in the real app I pull data with CoreData

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        if (collectionView == self.bottomCollectionView) {

        SetFormatCollectionViewCell *cell = (SetFormatCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier forIndexPath:indexPath];

        cell.text = @"toto";

        return cell;
    }
    if (collectionView == self.collectionView) {

        TrackingSetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TrackingSetCollectionViewCellIdentifier forIndexPath:indexPath];

        [self configureCell:cell atIndexPath:indexPath];

        return cell;
    }

    return nil;
}

Custom Cell class :

@interface SetFormatCollectionViewCell : UICollectionViewCell

@property (strong,nonatomic) NSString *text;

@end

@implementation SetFormatCollectionViewCell

{
    FormatLabel *aFormatLabel;
}

-(id)initWithFrame:(CGRect)frame {

    if (self=[super initWithFrame:frame]) {

        // Initialization code

        aFormatLabel = [[FormatLabel alloc]initWithFrame:self.frame textColor:[UIColor blackColor] font:[UIFont fontWithName:@"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];
        [self.contentView addSubview:aFormatLabel];
    }
    return self;
}

-(void)prepareForReuse {

    [super prepareForReuse];
    self.text = @"";
}

-(void)setText:(NSString *)text {

    _text = [text copy];
    aFormatLabel.text = self.text;
}

FormatLabel Class (not important I think)

@interface FormatLabel ()
@property (assign,nonatomic) UIEdgeInsets edgeInsets;
@end

@implementation FormatLabel

-(id)initWithFrame:(CGRect)frame textColor:(UIColor *)color font:(UIFont *)font alpha:(CGFloat)alphaValue border:(BOOL)withBorder{

    self = [super initWithFrame:frame];
    if (self) {
        // Set up
        self.textAlignment = NSTextAlignmentCenter;
        self.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
        self.adjustsFontSizeToFitWidth = YES;

        self.textColor = color;
        self.alpha = alphaValue;
        self.font = font;
        if (withBorder) {
            self.layer.borderWidth = 1.0f;
            self.layer.borderColor = color.CGColor;
        }
        self.edgeInsets = UIEdgeInsetsMake(9, 6, 8, 6);
    }
    return self;
}

Thanks for helping

EDIT: for those who might have the same issue, I post 3 shots of the problem (you can find the answer just below). The 2nd shot contains colored cell the see the problem. The third shot is the one that I took just after accepting jmkk's answer.

Thanks for all the other answers!

enter image description here enter image description here enter image description here

Upvotes: 3

Views: 2793

Answers (1)

jmkk
jmkk

Reputation: 261

Your problem lies with positioning of the FormatLabel view within the cell. You're using cell's frame as the frame for the label, while what you need is the cell's bounds.

Cell's frame is relative to its superview, so applying the same position to cell's subview renders it offset relative to the cell itself.

Fix your code to do this:

 aFormatLabel = [[FormatLabel alloc]initWithFrame:self.bounds textColor:[UIColor blackColor] font:[UIFont fontWithName:@"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];

Upvotes: 3

Related Questions