繁体   English   中英

在表格视图中更改单元格高度目标C.

[英]Changing cell height in table view Objective C

我根据显示在其中的文本来改变单元格的高度。 文本会有所不同,我基本上希望细胞改变大小。 这是我到目前为止:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell1"];
    CGRect cellRectangle;
    if (cell == nil) {

    cellRectangle = CGRectMake(0.0, 0.0, 300, 110);
    cell = [[[UITableViewCell alloc] initWithFrame:cellRectangle reuseIdentifier:@"DefaultCell1"] autorelease];
    }

    UILabel *label;
    cellRectangle = CGRectMake(10, (40 - 20) / 2.0, 280, 110);

    //Initialize the label with the rectangle.
    label = [[UILabel alloc] initWithFrame:cellRectangle];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 20;

    label.font = [UIFont fontWithName:@"Helvetica" size:11.0];
    label.text = [[self.person.statusMessages objectAtIndex:indexPath.row]   valueForKey:@"text"];
    CGFloat height = [label.text sizeWithFont:label.font].height;

    //So right here is where I think I need to do something with height and 
    //see it to something tied to he cell

    [cell.contentView addSubview:label];

    [label release];

    return cell;
}

您不应该在cellForRowAtIndexPath方法中设置高度。 还有另一个名为tableView的UITableViewDatasource方法:heightForRowAtIndexPath:你应该实现它来返回单元格的高度。 但要小心使用它; 你不能在这个方法中获得该索引路径的单元格; 如果你这样做,你将构建一个无限的递归循环。

编辑:要清楚; 你需要根据它的内容计算细胞的高度,而不是单元本身。

此外,在创建单元格的代码中,您只需将CGRectZero传递给frame参数即可; 如果你这样做,表视图将计算单元格的框架(如果我没记错的话,在OS 3.0中也明确传入框架)。

如果所有行都具有相同的高度,我认为在你的情况下是正确的,你可以在tableview控制器中设置setRowHeight。

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization.
        NSLog(@"%s", __FUNCTION__);
        [self.tableView setRowHeight:110];
    }
    return self;
}

行宽可以通过表的超视宽度来控制

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM