Variable height UIViews and labels
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 appli开发者_运维技巧cation. 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?
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];
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.
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 ...
精彩评论