views:

94

answers:

3

Ok so I'm not in a table cell at this point. (I've seen a lot of threads regarding the height of cells.)

I have a detail view with a title and description at the top of the view, followed by a tableView below it. If the description is going to vary in length, how do I get the tableView to adjust with it accordingly so it always starts directly below it?

I see apple doing this in the app store application. If you look at an application description (as if you were going to buy it), they have an application description, and wherever that ends a scrolling pane of screenshots follow. How do they do the varying height on the text description like that?

Are they doing this all programmatically or can I use the layout controls in IB to do the same thing?

+1  A: 

you need to add table view programmatically in your view and set its frame as per your detail view's size

1> get the current frame of the detailView 2> add it's height and than add tableview to your view

like

UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(initX , initY + DetailViewFrame.size.height, TableWidth, TableHeight) style:UITableViewStylePlain];
[view addSubView:table];
mihirpmehta
This makes sense. Too bad you can't do this in IB somehow. I think I'll try and figure out how to make the whole thing one big scrolling pane.Appreciate the help.
Bob Spryn
Thanks... Just correcting my self... no need to add width in the X co ordinate... i am editing it...
mihirpmehta
+1  A: 

The layout controls in IB change how a view is sized when its parent view resizes. In this case, (I assume) the label and table view are sibling views, so you need to do this programatically. Having sizes the label to fit, find the bottom of it (the origin's y location plus the label's height) and use that to guide where you place the table view. You would probably do this in -viewDidLoad or -viewWillAppear:, depending on when you have enough information to do the calculation.

Graham Lee
A: 

I think you need to use the "sizeWithFont" to calculate the cell height at run time, see following :

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
  CGSize labelSize = CGSizeMake(200.0, 20.0);
  if ([string length] > 0)
    labelSize = [string sizeWithFont: [UIFont boldSystemFontOfSize: 17.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
  return 24.0 + labelSize.height;
}

And in "cellForRowAtIndexPath"

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;       
    }

cell.textLabel.text = @"Your string with variable length";              
        [cell.textLabel setFont:[UIFont boldSystemFontOfSize:12]];
        //[cell.textLabel setAdjustsFontSizeToFitWidth:YES]; 
        [cell.textLabel setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines]; 
        [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
        [cell.textLabel setNumberOfLines:0];
return cell;
}

Hope this helps you ...

Ansari
Thanks for the help on that, but in this case I'm not in a table cell. Its the placement of the table view itself based on the height of a text label above it. Good for reference though.
Bob Spryn