Dancer
Dancer

Reputation: 17651

UITableView with Various Custom Cells of differing heights within UIView

I am building a IOS app homepage which basically consists of the following structure -

enter image description here

Which is basically a UIView which contains a nested UIScrollView which inturn contains a TableView with 3 Custom Cells.

I pull data out of a dynamic array and filter it into the relevant cell - all works fine other than two issues I cant work out -

1- The custom cells are different heights in the storyboard but when the app is compiled they are always the same height - is it possible to set an auto height on the UITableView Row? If not can anyone explain how I can apply the correct height to each cell?

2- The TableView / View which wraps the table view need to expand to make all dynamic cells visible - how can I achieve this?

below is my tableview method for reference -

-(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"CellFeed";
    static NSString *CellIdentifierArtNo =@"CellArtRecNo";
    static NSString *CellIdentifierBook =@"CellBooking";
    UITableViewCell *cell;
    feedData *f = [self.HpFeedArray objectAtIndex:indexPath.section];

    NSString * ArtPString = @"articleP";
    NSString * ArtNoPString = @"article";
    NSString * ArtBook = @"booking";

    if([f.FeedGroup isEqualToString:ArtPString]){
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier  
                                               forIndexPath:indexPath];
        CellHp_RecArticleWithImage *cellImage = (CellHp_RecArticleWithImage *)cell;
        cellImage.selectionStyle = UITableViewCellSelectionStyleNone;
        cellImage.artTitle.text = f.FeedTitle;
        cellImage.artImg.text = f.FeedDesc;
        cellImage.artDate.text = f.FeedDate;
        cellImage.textLabel.backgroundColor=[UIColor clearColor];

        return cellImage;
    }
    if([f.FeedGroup isEqualToString:ArtBook]){
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierBook 
                                               forIndexPath:indexPath];
        CellHp_BookingAlert *cellBook = (CellHp_BookingAlert *)cell;
        cellBook.selectionStyle = UITableViewCellSelectionStyleNone;
        cellBook.HeadlineLbl.text = f.FeedTitle;
        cellBook.TextBoxLbl.text = f.FeedDesc;
        //cellBook.DateLbl.text = f.FeedDate;

        return cellBook;
    }
    else{
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierArtNo 
                                               forIndexPath:indexPath];
        CellHp_RecArticleNoImage *cellNoImage = (CellHp_RecArticleNoImage *)cell;
        cellNoImage.selectionStyle = UITableViewCellSelectionStyleNone;
        cellNoImage.artTitle.text = f.FeedTitle;
        cellNoImage.artImg.text = f.FeedDesc;
        cellNoImage.artDate.text = f.FeedDate;
        cellNoImage.textLabel.backgroundColor=[UIColor clearColor];

        return cellNoImage;
    }

Cheers

Upvotes: 0

Views: 213

Answers (3)

Shinigami
Shinigami

Reputation: 2213

It looks like you've got custom UITableViewCell subclasses like CellHp_RecArticleNoImage or CellHp_BookingAlert (may I suggest renaming them to something a little less headache-inducing? ;) ).

In that case, if you only have 3 different cell types, you could do this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = [tableView cellForAtIndexPath:indexPath];

    if ([cell isKindOfClass:[CellHp_BookingAlert class])
    {
         return 123.0;
    }
    else if ([cell isKindOfClass:[CellHp_RecArticleNoImage class])
    {
         return ...
    }
     ...
}

Or, you could make all your custom classes implement a height method for a cleaner solution and in heightForRowAtIndexPath: you could just get the cell and call its height method.

Re. Question 2, use UIView's sizeToFit method as Tim mentioned.

Upvotes: 1

Timothy Moose
Timothy Moose

Reputation: 9915

  1. You can derive static or dynamic (data driven) cell heights from the storyboard by dequeuing them in heightForRowAtIndexPath according to the procedure outlined here: Retrieve custom prototype cell height from storyboard?

  2. I'm not exactly sure I understand this question, but you can get the contentSize of the table view if you're trying to size the table to fit the content.

Upvotes: 0

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

You need to implement below method to identify the height of cell.

- (CGFloat)tableView:(UITableView *)tableView 
  heightForRowAtIndexPath:(NSIndexPath 
   *)indexPath;

Upvotes: 1

Related Questions