Ios – How to calculate heightForRowAtIndexPath for cells which setup via switch in cellForRowAtIndexPath.

iosiphoneobjective-cuitableview

I setup my cells like this:

So, a couple of switches, define the cells, because the data is not in the list of objects, but a set of information which should be displayed in a tableView in some different ways.

-(UITableViewCell *)value1CellForTableView:(UITableView *)tableView {
    static NSString *CellIdentifierValue1 = @"Value1Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierValue1];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifierValue1];
        cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.textAlignment = UITextAlignmentLeft;
    }
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    switch (indexPath.section) {
        case 0:
            //Coupon
            switch (indexPath.row) {
                case 0:
                    //Couponcode
                    cell = [self value1CellForTableView:tableView];
                    cell.textLabel.text = @"Code";
                    cell.detailTextLabel.text = presentedCoupon.couponNr;
                    break;
                case 1:
                    //Coupondescription
                    cell = [self value1CellForTableView:tableView];
                    cell.detailTextLabel.text = presentedCoupon.couponDescription;
                    cell.detailTextLabel.numberOfLines = 0;
                    cell.textLabel.text =@"Ihr Vorteil";
                    break;

            }        
            break;


        case 1:
            //Productinfo
            switch (indexPath.row) {
                case 0:
                    cell = [self defaultCellForTableView:tableView];
                    cell.imageView.image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath]  stringByAppendingPathComponent: [presentedCoupon.refProdName stringByAppendingString:@".png"]]];
                    cell.textLabel.text = presentedCoupon.refProdName;
                    break;



            }
            break;  

        case 2:
            //Shopinfo
            switch (indexPath.row) {
                case 0:
                    cell = [self defaultCellForTableView:tableView];
                    cell.textLabel.text = ((Shop*)presentedCoupon.refShop).name;
                    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

                    break;    
            }
            break;       
    }

    if (cell == nil) {
        cell = [self defaultCellForTableView:tableView];
        cell.textLabel.text = @"Stanni";
    }
    [cell layoutIfNeeded];
    return cell;
}

And i calculate the height like this.

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"height");
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

    CGFloat height = 24 + [cell.detailTextLabel.text sizeWithFont:cell.detailTextLabel.font constrainedToSize: CGSizeMake(cell.detailTextLabel.frame.size.width, 1000.0f) lineBreakMode:cell.detailTextLabel.lineBreakMode].height;

    return MAX(height, 44.0f);

Problem:

The problem is, as mentioned in many threads, and also visible in my log, that the height of each cell, (visible or not) is asked at the initialization of the table view. So in bigger 100+ lists, also 100+ cells are created –> wasted at startup.

Ist there another possibility to get this information when the cell is set up like this? Is it really neccessary to built the switch case sturcture again in heightForRowAtIndexPath to avoid these calls and though get the right heights for each cell?

Would it be better to hold a "datasource-list" with the single information of each cell?

But how to handle the different cell styles, custom cells.

Best Solution

The method

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

is called for each cell before the method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

So in the first method, cells are not yet created, and you should not try to access them.

When you create a tableView, the data source is first asked for the number of row. Then for each row, the data source is asked for the height of the row, so that the tableView know the total height of it's content, and then finally, the data source is asked for the cell (actual view) to display.

In your case, i would built the switch case structure again. Concerning the "datasource-list", I have never done it before, so maybe it's a better solution.

Related Question