2011-03-01 130 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