Shahar
Shahar

Reputation: 3692

Show UICollectionView with multiple different UICollectionViewCells types

I am trying to create a UICollectionView that has different UICollectionViewCells i have few items, one have 3 UILabel and UIIMageView, other has 2 UIImageViews and 6 UILables etc.

i'm trying to init the cell and add the views for every specific scenario, but that dosen't seem to work. i'm checking if cell==nil and then add the views, (i do get inside the 'if' statement).

if i use 'addSubView' inside the if statement, it does nothing

ResumeFeedCell *cell= (ResumeFeedCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"ResumeFeedCell" forIndexPath:indexPath];
if (!cell) {
    //init views in the cell and add them
    UIImageView *mediaImage =[[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,530, 560)];
    mediaImage.tag = 12;
    [cell.contentView addSubview:mediaImage];

    UIImageView *typeImage = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,150, 150)];
    typeImage.tag = 11;
    [cell.contentView addSubview:typeImage];

    UILabel *textLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 530, 560)];
    textLable.tag = 10;
    [cell.contentView addSubview:textLable];

}

UIImageView *mediaImage = (UIImageView*)[cell.contentView viewWithTag:12];
UIImageView *typeImage = (UIImageView*)[cell.contentView viewWithTag:11];
UILabel *textLable = (UILabel*)[cell.contentView viewWithTag:10];

textLable.text = _bio;
textLable.textAlignment = NSTextAlignmentCenter;
textLable.numberOfLines = 0;
textLable.tag = 10;
textLable.adjustsFontSizeToFitWidth=YES;
textLable.minimumScaleFactor=0.5;

[mediaImage setImageWithURL:[NSURL URLWithString:getImageUrlwithIdAndSize([[[_datasource objectAtIndex:indexPath.row] objectForKey:@"media"] objectAtIndex:0], 1)]];

i tried a different way, like that:

UIImageView *mediaImage = (UIImageView*)[cell.contentView viewWithTag:12];
if(!mediaImage)mediaImage = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,530, 560)];
mediaImage.tag = 12;
[cell.contentView addSubview:mediaImage];


UIImageView *typeImage = (UIImageView*)[cell.contentView viewWithTag:11];
if(!typeImage)typeImage = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,150, 150)];
typeImage.tag = 11;
[cell.contentView addSubview:typeImage];

UILabel *textLable = (UILabel*)[cell.contentView viewWithTag:10];

if (!textLable)
    textLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 530, 560)];

which seems to work OK, but i think it's a bit ugly and memory consuming. because it adds the subviews for every item, which makes it not reusable...

i saw THIS ANSWER but i want to customize cellForItemAtIndexPath.

any help will be appreciated. Tx, Shahar.

Upvotes: 0

Views: 366

Answers (1)

Shahar
Shahar

Reputation: 3692

Answering my own question.

This chunk of code works great! I had to clean and rebuild the project.

turns out, that even after running the project 200 times, it wont compile it as it should.

In conclusion, Clean project if something doesn't works, it's not always the code.

Upvotes: 1

Related Questions