2011-03-01 101 views
21

如何让UITableView行高自动调整到UITableViewCell的大小?因此,假设我在Interface Builder中创建了UITableViewCell,并且它的高度超过了标准大小,我如何才能让UITableView行高度自动调整大小呢? (即与手动测量界面构建器中的高度并以编程方式设置高度相反)如何让UITableView行高自动调整到UITableViewCell的大小?

+0

你可以在这里为iOS 7及更高版本找到答案。 http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Collin 2015-07-08 22:39:07

回答

31

如果所有单元格都相同,请将UITableView上的rowHeight属性设置为您单元格的大小。如果它们基于内容而全部不同,则必须实施-tableView:heightForRowAtIndexPath:并根据数据源计算每行的高度。

+4

所以没有魔术子弹重新有ios工作这对你自动呢? – Greg 2011-03-01 23:46:27

+0

不幸的是目前还没有。 – 2011-03-01 23:48:38

+1

Brian的答案给出了一个很好的一步一步实现tableView:heightForRowAtIndexPath – 2013-05-13 21:26:17

13

在带有tableview的xib中,可以添加一个单元对象并将其链接到源代码中的IBOutlet。你不会在任何地方使用它,你只是用它来获得细胞的高度。

然后,在tableView:heightForRowAtIndexPath:中,您可以使用该对象来获取高度。它不是100%自动的,但至少这样可以节省您在IB中单元格视图中进行更改时手动更新源代码的麻烦。


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return myDummyCellObject.bounds.size.height; 
} 

如果所有的行具有相同的类型(小区)的可以编程设置tableView.rowHeight属性,而不是实施上述委托方法的。取决于你的情况。

哦,并确保你不要忘记释放-dealloc myDummyCellObject。

+1

如果你已经添加一个属性作为IBOutlet,你不会释放它。您不再负责管理它。但现在使用ARC,所有这些dealloc的东西或多或少都没有实际意义。 – Kaili 2014-01-30 19:45:30

37

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/是一个很好的教程。

主要位

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    // Get the text so we can measure it 
    NSString *text = [items objectAtIndex:[indexPath row]]; 
    // Get a CGSize for the width and, effectively, unlimited height 
    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 
    // Get the size of the text given the CGSize we just made as a constraint 
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 
    // Get the height of our measurement, with a minimum of 44 (standard cell size) 
    CGFloat height = MAX(size.height, 44.0f); 
    // return the height, with a bit of extra padding in 
    return height + (CELL_CONTENT_MARGIN * 2); 
} 
+2

这个答案是金钱。在几分钟内就能完成工作。谢谢。 – 2013-05-13 21:26:45

+0

这是人们所期望的:-) – Akshay 2014-04-08 07:06:29

+0

我的自定义单元格具有各种属性(例如ImageView,Label,TextView)。在这种情况下,我的'NSString * text = [items objectAtIndex:[indexPath row]]是什么? – 2014-07-18 21:52:49

-1
self.tableView.estimatedRowHeight = 65.0; // Estimate the height you want 
self.tableView.rowHeight = UITableViewAutomaticDimension; // auto change heights for multiple lines cells. 

当执行需要UITableViewDataSource方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = [self.todoArray objectAtIndex:indexPath.row]; 
    cell.textLabel.numberOfLines = 0; // allow multiple lines showing up 

    return cell; 
} 
+0

此逻辑不应位于'cellForRowAtIndexPath'方法中。 – Pontus 2017-11-17 12:38:32