可变高度UIViews和标签

时间:2010-06-17 10:45:55

标签: iphone objective-c uiview

好的,所以我现在不在桌子上。 (我已经看到很多关于细胞高度的线索。)

我有一个详细视图,在视图的顶部有一个标题和描述,后面跟着一个tableView。如果描述的长度不同,我如何让tableView相应调整,以便它始终直接从它下面开始?

我看到苹果在app store应用程序中这样做。如果您查看应用程序描述(就像您要购买它一样),它们会有一个应用程序描述,无论在哪里结束,都会显示一个滚动屏幕截图。他们如何在文本描述中做出不同的高度?

他们是以编程方式执行此操作还是可以使用IB中的布局控件来执行相同的操作?

3 个答案:

答案 0 :(得分:1)

您需要在视图中以编程方式添加表格视图,并根据详细视图的大小设置其框架

1>获取detailView的当前帧 2 - ;添加它的高度,然后将tableview添加到视图中

喜欢

UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(initX , initY + DetailViewFrame.size.height, TableWidth, TableHeight) style:UITableViewStylePlain];
[view addSubView:table];

答案 1 :(得分:1)

IB中的布局控件会在其父视图调整大小时更改视图的大小。在这种情况下,(我假设)标签和表视图是兄弟视图,因此您需要以编程方式执行此操作。使标签尺寸适合,找到它的底部(原点的y位置加上标签的高度)并使用它来指导放置表格视图的位置。您可能会在-viewDidLoad或-viewWillAppear:中执行此操作,具体取决于您何时有足够的信息进行计算。

答案 2 :(得分:0)

我认为您需要使用“sizeWithFont”来计算运行时的单元格高度,请参阅以下内容:

- (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;
}

在“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;
}

希望这可以帮助你...