如何让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