2013-12-17 65 views
1

我有一个UIViewController,其中有3个UITableView垂直排列。我不希望任何表格滚动。两个表格(AB)的高度正在根据其内容进行计算 - 第三个(C)将重新调整大小以适应屏幕上留下的任何空间。对于C,我会知道需要显示多少行,并且我想计算正确的heightForRowAtIndexPath以使所有单元格都适合。计算固定行数和表格高度的行高

现在,我想

  • 把整个屏幕的高度
  • 减去关闭总高度为A
  • 减去关闭总高度为B
  • 减关闭一些固定的空白空间
  • 减去大小C的标题
  • 减去关闭C的页脚
  • 鸿沟的大小由细胞的数量C

从我如何计算,这应该让我的身高,我需要为C每个单独的小区。 这是正确的吗?有什么我失踪?下面是我使用的代码:

- (CGFloat) tableView:(UITableView *) heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (tableView = self.tableC) 
    { 

     // self.lcA & self.lcB are NSLayoutContraints that I'm setting after I calculate how tall those tables need to be 
     // buffer is some white space padding I add to keep the tables from touching - it will be hardcoded to some constant before this code runs 

     CGFloat target = (self.view.bounds.size.height - self.lcA.constant - self.lcB.constant - [self tableView:tableView heightForHeaderInSection:indexPath.section] - [self tableView:tableView heightForFooterInSection:indexPath.section] - buffer)/[self tableView:tableView numberOfRowsInSection:indexPath.section]; 

    } 

} 

然而,当我运行它,它崩溃了,说-[MyViewController tableView:heightForHeaderInSection:]: unrecognized selector sent to instance

我没有明确这个特定视图控制器上执行heightForHeaderInSection,但控制器设置作为<UITableViewDataSource,UITableViewDelegate,...>MyViewController.h文件中。 为了使用此代码,我需要实现heightForHeaderInSection吗?如果是这样,是否有一种方法可以实现它,并强制它返回一些默认值(如return [super heightForHeader];)?如果没有,为什么我得到这个错误?

回答

1

而不是做 “ - [自我的tableView:的tableView heightForFooterInSection:indexPath.section]” 和所有的东西,我想你可以通过这个访问头高度:

self.tableA.sectionHeaderHeight 

的页脚:

self.tableA.sectionFooterHeight 

还您有:

self.tableA.tableHeaderView 
self.tableA.tableFooterView 

你可以拥有这一切更好看苹果DOC使用:TableView Doc

0

我从来没有真正直接试过这个,但这可能会给你一个方向去。 UITableViewUIScrollView下降。因此,您可以使用UIScrollView的所有属性,例如scrollEnabled防止滚动和contentSize

+0

是的,我使用'contentSize'来设置我在发布的代码中提到的NSLayoutConstraint,并且我选中了禁止在Storyboard上滚动的框,所以这也被照顾了。我正在寻找的是如何缩小表格“C”中的单元格,使它们都适合我拥有的空间。我认为'UIScrollView'上没有任何东西可以帮助我,除非我错过了一些东西。 – GeneralMike

1

headerViewForSection是UITableView的方法,因此您需要在您的控制器中调用它,但在tableView中调用它。 您也可以在您的控制器中实现它,以返回您需要使用UITableViewAutomaticDimension作为返回值的默认值。这个常量将从iOS 5开始工作

+0

+1'UITableViewAutomaticDimension' - 我从来没有听说过。它似乎并没有工作,虽然 - 我只是做'返回UITableViewAutomaticDimension;'(这是唯一的行在我的'heightForHeaderInSection:')。我加入了一些'NSLog',它只是返回'-1.00000' – GeneralMike