2010-02-12 99 views
2

我是iphone开发新手。我解析一个xml文件,在表格的每一行显示标题,日期,视图和摘要。summar的内容很大,所以只有前三个单词显示在单元格中。我怎样才能增加行的高度方面的内容的长度。所有的内容应该适合在单元格内,并且应该显示全部内容。请帮助我。谢谢。如何在iphone中增加tableview中单元格的高度?

回答

13

您可以在tableView.delegate /数据源做到这一点:

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    switch (indexPath.row){ 
     case 0: 
     if(indexPath.section==0) 
      return 123.0; // first row is 123pt high 
     default: 
     return 40.0; // all other rows are 40pt high 
    } 
} 

但这不过减慢滚动性能。理想情况下,您应该使用tableview.rowHeight将所有行的行高设置为相同。

+0

我想增加显示摘要的单个行的高度 – Warrior 2010-02-12 15:23:45

+0

根据NSIndexPath返回不同的数字。 – FelixLam 2010-02-12 15:26:34

+0

你能否详细说明你的答案吗?因为我是iphone新手,我无法理解它。 – Warrior 2010-02-12 15:29:16

0

一种方法做到这一点是手动设置单元格的大小。只需用适当的东西替换ROW_HEIGHT即可。

cell.frame = CGRectMake(0.0, 0.0, 320.0, ROW_HEIGHT); 
+0

我想改变只有一行而不是所有行的高度。 – Warrior 2010-02-12 15:19:33

+0

您只能为此单元格执行此操作... – willcodejavaforfood 2010-02-12 15:27:46

2
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *text = @"write your dynamic text here"; 
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0]; 
    CGSize constraintSize = CGSizeMake(320.0f, 999); // Make changes in width as per your label requirement. 

    CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return textSize.height; 
} 
1

,如果你有在一个视图中控制器的多条的tableview,可以为每个不同的表视图,作为以下方法做。

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

    if (tableView == self.tableViewCampaigns) { 

     return 45; 
    }else if(tableView == self.tableViewAgenda) 
     return 32; 
    else 
     return 29; 
} 
0

尝试使用此代理根据单元格内容自动管理。

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
      return 100 
     } 

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
      return UITableViewAutomaticDimension 
     } 
相关问题