views:

62

answers:

1

Hello, just a quick question. Is there a SIMPLE way to dynamic set the cell height depending on the text that is contained in the cells? Scenario: grouped table view with two sections. sections one and two contain text from a database (with different lengths of text).

I have looked into some tutorials and code snippets but nothing seems to be of any help to me (or i m too thick to get it right).

I would appreciate some advise or a link to a tutorial or even better some coding ideas!

thank you!

A: 

Ok here is the answer: (well not completed though) i have a problem with the it and would appreciate some help as it is driving me crazy! (I get a WARNING: Control reaches end of non-void function)

Can someone help here?

here is the code!

#pragma mark -
#pragma mark UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {



    UITableViewCell *cell = [self newOrReusableCell];

    if (indexPath.section == A_SECTION) {

    NSUInteger row = indexPath.row;
    NSString *text = [_centerArray objectAtIndex:row];
    UILineBreakMode lineBreakMode = cell.textLabel.lineBreakMode;

    CGFloat width = _tableView.contentSize.width - (kTableCellHPadding*2 + tableCellMargin*2);
    UIFont* font = cell.textLabel.font;
    CGSize size = [text sizeWithFont:font
                   constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                       lineBreakMode:lineBreakMode];

    if (size.height > kMaxLabelHeight) {
        size.height = kMaxLabelHeight;
    }

        return size.height + kTableCellVPadding*2;
}


    if (indexPath.section == B_SECTION) {

        NSUInteger row = indexPath.row;
        NSString *text = [_leftArray objectAtIndex:row];
        UILineBreakMode lineBreakMode = cell.textLabel.lineBreakMode;

        CGFloat width = _tableView.contentSize.width - (kTableCellHPadding*2 + tableCellMargin*2);
        UIFont* font = cell.textLabel.font;
        CGSize size = [text sizeWithFont:font
                       constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                           lineBreakMode:lineBreakMode];

        if (size.height > kMaxLabelHeight) {
            size.height = kMaxLabelHeight;
        }

    return size.height + kTableCellVPadding*2;  
}
}//i get the WARNING here!!
treasure
You get a warning because you have 2 if statements with returns in them, and nothing after. If both if statements are false for any reason, you'll have no return. You can fix this warning by putting a return at the bottom of the method before the last }, even if it's just return 0.0;
Cory Imdieke