在使用自定义UITableView作为节标题的纯UIView's中,有一种方法可以计算:

  • 当“节”之一位于顶部时,该节与下一个节之间的距离是多少?

  • 我希望在这里计算:
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    

    最佳答案

    您可以通过从tableView:numberOfRowsInSection:协议调用UITableViewDataSourceDelegate方法来找到该部分中的行数。您可以使用tableView:heightForRowAtIndexPath:协议中的UITableViewDelegate方法获取该部分中每一行的高度。将所有行的高度加起来,就可以得到所需的距离。

    假设您有对tableview和section的引用,您的代码将看起来像这样。

    float totalHeight = 0;
    
    for (int i = 0; i < [tableViewDataSourceDelegate
                                           tableView:tableView
                               numberOfRowsInSection:section]; i ++) {
        totalHeight += [tableViewDelegate
                                tableView:tableView
                  heightForRowAtIndexPath:[NSIndexPath
                                       indexPathForRow:i
                                             inSection:section]];
    }
    

    还没有机会测试此代码,但它应该可以工作[tm]。

    编辑

    仅当标题位于顶部时,这才起作用。

    09-16 07:36