UITableView内容高度
我有一个设置为不启用滚动的UITableView,它存在于UIScrollView中。我这样做是因为设计规范要求一些看起来像表视图的东西(实际上有两个并排),实现表视图比添加一大堆按钮(分组表视图)要容易得多 问题是,我需要知道scrollview的容器视图有多大,所以它可以滚动表格视图的整个高度。加载后,是否有任何方法可以找到tableview的高度?没有像滚动视图那样的contentView属性,框架似乎是静态的,等等 有什么想法吗?使用UITableView内容高度,uitableview,height,Uitableview,Height,我有一个设置为不启用滚动的UITableView,它存在于UIScrollView中。我这样做是因为设计规范要求一些看起来像表视图的东西(实际上有两个并排),实现表视图比添加一大堆按钮(分组表视图)要容易得多 问题是,我需要知道scrollview的容器视图有多大,所以它可以滚动表格视图的整个高度。加载后,是否有任何方法可以找到tableview的高度?没有像滚动视图那样的contentView属性,框架似乎是静态的,等等 有什么想法吗?使用 CGRect lastRowRect= [table
CGRect lastRowRect= [tableView rectForRowAtIndexPath:index_path_for_your_last_row];
CGFloat contentHeight = lastRowRect.origin.y + lastRowRect.size.height;
然后,您可以使用contentHeight变量设置scrollView的contentSize。更通用的解决方案适用于我:
CGFloat tableViewHeight(UITableView *tableView) {
NSInteger lastSection = tableView.numberOfSections - 1;
while (lastSection >= 0 && [tableView numberOfRowsInSection:lastSection] <= 0)
lastSection--;
if (lastSection < 0)
return 0;
CGRect lastFooterRect = [tableView rectForFooterInSection:lastSection];
return lastFooterRect.origin.y + lastFooterRect.size.height;
}
CGFloat表格视图高度(UITableView*tableView){
NSInteger lastSection=tableView.numberOfSections-1;
而(lastSection>=0&&[tableView numberOfRowsInSection:lastSection]UITableView
是UIScrollView
的一个子类,因此它有一个contentSize
属性,您应该可以毫无问题地使用它:
CGFloat tableViewContentHeight = tableView.contentSize.height;
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, tableViewContentHeight);
然而,正如问题所指出的,当您对表视图进行更新(如插入一行)时,它的contentSize
似乎不会像UIKit中大多数其他动画大小调整那样立即更新。在这种情况下,您可能需要求助于Michael Way的答案。(虽然我认为在UITableView
上作为一个类别来实现更为合理)您可以浏览各个部分,并使用rectForSection
来计算总高度(包括页脚和页眉!)。在swift中,我在UITableView
上使用以下扩展名
extension UITableView {
/**
Calculates the total height of the tableView that is required if you ware to display all the sections, rows, footers, headers...
*/
func contentHeight() -> CGFloat {
var height = CGFloat(0)
for sectionIndex in 0..<numberOfSections {
height += rectForSection(sectionIndex).size.height
}
return height
}
}
扩展UITableView{
/**
计算要显示所有节、行、页脚和页眉时所需的桌面视图总高度。。。
*/
func contentHeight()->CGFloat{
变量高度=CGFloat(0)
对于0中的sectionIndex..在这里的情况相同。对于具有自动调整单元格大小的表似乎不起作用。