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

I created the UICollectionView programmatically. I am using a custom UICollectionViewCell subclass. Inside the class, I create a shortcut with my own class (it's easier and faster to customize its appearance). The problem I am facing is this: For multiple cells, the CollectionView does not break the contents of the labels. I know the data is here (print to console), that is, the text property of the cell contains the string data that I want to show, but for some reason the CollectionView is not displaying the contents of the label. I tried with a simple test (print "toto" inside a label) and I get a few toto here and there, but not in all cells. As you can see, I have 2 UICollectionViews inside one ViewController and that is why I am checking if it is 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 data source (dumb test with value "toto") in real application I am fetching data using CoreData p>

- (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 guess)

@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 the help

EDIT: For those who might have the same problem, I am posting 3 snapshots of the problem (you can find the answer just below). The second shot contains a colored cell when seeing the problem. The third snapshot is the one I took right after accepting jmkk's answer.

Thanks for all the other answers!

enter image description hereenter image description hereenter image description here

+3


source to share


1 answer


Your problem is the positioning of the View FormatLabel inside the cell. You are using the cell's frame as the label's frame, while you want the cell borders.

A cell in a cell is supervised by it, so applying the same position to a subzone of cells makes it offset from the cell itself.



Correct 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];

      

+3


source







All Articles