如何讓UITableView行高自動調整到UITableViewCell的大小?因此,假設我在Interface Builder中創建了UITableViewCell,並且它的高度超過了標準大小,我如何才能讓UITableView行高度自動調整大小呢? (即與手動測量界面構建器中的高度並以編程方式設置高度相反)如何讓UITableView行高自動調整到UITableViewCell的大小?
回答
如果所有單元格都相同,請將UITableView
上的rowHeight
屬性設置爲您單元格的大小。如果它們基於內容而全部不同,則必須實施-tableView:heightForRowAtIndexPath:
並根據數據源計算每行的高度。
所以沒有魔術子彈重新有ios工作這對你自動呢? – Greg 2011-03-01 23:46:27
不幸的是目前還沒有。 – 2011-03-01 23:48:38
Brian的答案給出了一個很好的一步一步實現tableView:heightForRowAtIndexPath – 2013-05-13 21:26:17
在帶有tableview的xib中,可以添加一個單元對象並將其鏈接到源代碼中的IBOutlet。你不會在任何地方使用它,你只是用它來獲得細胞的高度。
然後,在tableView:heightForRowAtIndexPath:
中,您可以使用該對象來獲取高度。它不是100%自動的,但至少這樣可以節省您在IB中單元格視圖中進行更改時手動更新源代碼的麻煩。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return myDummyCellObject.bounds.size.height;
}
如果所有的行具有相同的類型(小區)的可以編程設置tableView.rowHeight
屬性,而不是實施上述委託方法的。取決於你的情況。
哦,並確保你不要忘記釋放-dealloc
myDummyCellObject。
如果你已經添加一個屬性作爲IBOutlet,你不會釋放它。您不再負責管理它。但現在使用ARC,所有這些dealloc的東西或多或少都沒有實際意義。 – Kaili 2014-01-30 19:45:30
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);
}
這個答案是金錢。在幾分鐘內就能完成工作。謝謝。 – 2013-05-13 21:26:45
這是人們所期望的:-) – Akshay 2014-04-08 07:06:29
我的自定義單元格具有各種屬性(例如ImageView,Label,TextView)。在這種情況下,我的'NSString * text = [items objectAtIndex:[indexPath row]]是什麼? – 2014-07-18 21:52:49
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;
}
此邏輯不應位於'cellForRowAtIndexPath'方法中。 – Pontus 2017-11-17 12:38:32
- 1. 動態調整UITableView高度的大小
- 2. 自動調整大小自定義UITableViewCell
- 3. UITextField UITableViewCell中的自動調整大小
- 4. 調整大小的UITableViewCell到
- 5. 如何自動調整UITableView的高度?
- 6. 關閉UITableView自動調整大小
- 7. 動畫UITableView調整大小
- 8. 滾動/調整大小UITableView
- 9. UITableview調整動畫大小
- 10. 如何停止自動調整UITableViewCell動畫大小?
- 11. 如何讓UITableView在只有一行時調整大小
- 12. 將調整大小UITableViewCell自動調整其contentView?
- 13. 如何調整UITableViewCell的imageview大小
- 14. 如何調整UITableViewCell的文本大小?
- 15. System.Windows.Forms.ToolStrip高度或自動調整大小
- 16. 調整大小自動iframe高度
- 17. Div高度自動不調整大小?
- 18. Swiper高度自動調整大小
- 19. 調整窗口大小時自動調整TableLayoutPanel行的大小
- 20. 我如何根據CustomCell的高度來調整UITableViewCell的大小?
- 21. 調整UITableViewCell的大小
- 22. 如何獲得一個UITableView自動調整大小?
- 23. 如何調整UITableView標題的大小?
- 24. AutoCompletionTextView行高調整大小
- 25. jquery行高度動態調整大小
- 26. 如何自動調整iFrame的大小?
- 27. 如何自動調整iFrame的大小?
- 28. 如何自動調整jPanel的大小
- 29. 如何自動調整UITextView的大小?
- 30. 如何自動調整GridView的大小
你可以在這裏爲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