Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 检测UITableView节标题之间的距离_Iphone_Ios_Uitableview - Fatal编程技术网

Iphone 检测UITableView节标题之间的距离

Iphone 检测UITableView节标题之间的距离,iphone,ios,uitableview,Iphone,Ios,Uitableview,在将自定义UIView的作为节头的普通UITableView中,是否有方法计算: 当其中一个区段位于顶部时,该区段与下一个区段之间的距离是多少 我希望在这里计算: - (void)scrollViewDidScroll:(UIScrollView *)scrollView 通过从UITableViewDataSourceDelegate协议调用tableView:numberofrowsinssection:方法,可以找到该节中的行数。您可以使用UITableViewDelegate协议中

在将自定义
UIView的
作为节头的普通
UITableView
中,是否有方法计算:

  • 当其中一个区段位于顶部时,该区段与下一个区段之间的距离是多少
我希望在这里计算:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

通过从
UITableViewDataSourceDelegate
协议调用
tableView:numberofrowsinssection:
方法,可以找到该节中的行数。您可以使用
UITableViewDelegate
协议中的
tableView:heightforrowatinexpath:
方法获取节中每一行的高度。将所有行的高度相加,就得到了所需的距离

假设您有一个对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]];
}
float totalHeight=0;
对于(int i=0;i<[tableViewDataSourceDelegate
tableView:tableView
numberOfRowsInSection:section];i++){
totalHeight+=[tableViewDelegate
tableView:tableView
RowAtIndexPath的高度:[NSIndexPath
indexPathForRow:我
第3节]];
}
还没有机会测试这段代码,但它应该可以工作[tm]

编辑

这仅在标题位于顶部时有效。

(假设所有行的高度相同)


我通过以下方法解决了这个问题:

  • 在创建节标题之前,请检查是否具有给定节的节标题。如果您确实从
    NSMutableArray
    返回它。如果没有,请继续
  • 创建节标题时,请在
    NSMutableArray
    中保留对其的引用

  • 当您在中滚动时:

-(无效)scrollViewDidScroll:(UIScrollView*)scrollView

请执行以下操作:

// Get the toppest section
NSUInteger sectionNumber = [[self.tableView indexPathForCell:[[self.tableView visibleCells] objectAtIndex: 0]] section];

// Get a reference to it
SFBasicSectionHeader *topHeader = [arrayOfWeakHeaders objectAtIndex:sectionNumber];

SFBasicSectionHeader *bellowHeader;
// Check if it's Ok to get the bellow header
if (sectionNumber+1<[arrayOfWeakHeaders count] && [arrayOfWeakHeaders objectAtIndex:sectionNumber +1])
{
    bellowHeader = [arrayOfWeakHeaders objectAtIndex:sectionNumber+1];
}

完成了

试试Numofcells部分*细胞高度您想做什么?滚动到某个事件的下一节?如果节彼此靠近(对于给定的空间,例如10p)我想在顶部的节头上做点什么。@MikePallard我本来希望它能工作的
headerViewForSection
,但是节头视图总是
nil
@JackyBoy如何将自定义节头视图存储在一个数组中,并根据每个表视图的contentOffset检查节视图的框架它何时滚动?这样,您就可以随时访问节标题视图。此链接显示了如何获取。基于此,您可以计算下一个剖面视图将可见的是哪个,以及以px为单位的距离。您忘记了一个事实,即当您滚动浏览某个剖面时,剖面标头会“粘”在顶部,因此您需要考虑最上面可见剖面内的当前位置。这不起作用。一个节标题只能显示3个单元格中的2个,请记住,在普通的
UITableView
上,当节有单元格要显示时,它会粘在顶部…承认我没有考虑到这一点。Maarten,你输入什么代码无关紧要,因为背后的理论是错误的。你有可变高度的行吗?可以检查每个表达式的返回值吗?
// Get the toppest section
NSUInteger sectionNumber = [[self.tableView indexPathForCell:[[self.tableView visibleCells] objectAtIndex: 0]] section];

// Get a reference to it
SFBasicSectionHeader *topHeader = [arrayOfWeakHeaders objectAtIndex:sectionNumber];

SFBasicSectionHeader *bellowHeader;
// Check if it's Ok to get the bellow header
if (sectionNumber+1<[arrayOfWeakHeaders count] && [arrayOfWeakHeaders objectAtIndex:sectionNumber +1])
{
    bellowHeader = [arrayOfWeakHeaders objectAtIndex:sectionNumber+1];
}
CGFloat differenceBetweenTopAndBellowSection = bellowHeader.frame.origin.y - topHeader.frame.size.height - self.tableView.contentOffset.y;