如何在多行TableView Cell中更改rowHeight?

时间:2011-06-10 16:19:37

标签: iphone objective-c

如何在多行TableView Cell中更改rowHeight?

  #define numberOfLines 2

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

    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
reuseIdentifier:CellIdentifier] autorelease];

    }

        tableView.rowHeight = (44.0 + (numberOfLines - 1) * 19.0); // this doesn't work.

    cell.textLabel.text = @"Main Test";

    cell.detailTextLabel.numberOfLines = numberOfLines;

    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;

    cell.detailTextLabel.text = @"Test\nTest2";

}

我想detailTextLabel有2行。但是,我也必须改变细胞的高度。

如果我像上面那样更改rowHeight,它就不起作用。

如果我改变如下,那就有效。

但maual表示,对于具有

的表格视图,它可能会导致严重的性能问题 数量众多。所以,我想直接更改rowHeight。我怎么能这样做?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath 
*)indexPath

{

    return (44.0 + (numberOfLines - 1) * 19.0);

}

1 个答案:

答案 0 :(得分:3)

您无法在

中更改或设置的高度
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
  function.

如果您的单元格具有可变高度,则必须使用heightForRowAtIndexPath方法。


如果您的单元格具有固定高度,则在创建rowHeight的实例时使用UITableView UITableView属性。

  [myTableView = UITableView alloc] initWithFrame:frame];
  .....................................................
  .....................................................
  myTableView.rowHeight = (44.0 + (numberOfLines - 1) * 19.0); 
  .....................................................
相关问题