Ios5 获取UITableView的当前固定部分

Ios5 获取UITableView的当前固定部分,ios5,uitableview,Ios5,Uitableview,获取UITableView中最顶端部分的标题或索引的最准确方法是什么?我不是指索引0处的节,而是指当前在滚动时固定在顶部的节标题。您可以在屏幕顶部获得该节,如下所示: NSUInteger sectionNumber = [[tableView indexPathForCell:[[tableView visibleCells] objectAtIndex:0]] section]; - (void)scrollViewDidEndDragging:(UIScrollView *)s

获取UITableView中最顶端部分的标题或索引的最准确方法是什么?我不是指索引0处的节,而是指当前在滚动时固定在顶部的节标题。

您可以在屏幕顶部获得该节,如下所示:

     NSUInteger sectionNumber = [[tableView indexPathForCell:[[tableView visibleCells] objectAtIndex:0]] section];
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate)
{
        // retrieve section here
        section = [[[self.tableview visibleCells] objectAtIndex:0] section];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
        // retrieve section here
        section = [[[self.tableview visibleCells] objectAtIndex:0] section];
}
但是,这种方法可能并不适合您,因为当它处于两个标头之间的过渡期时,它将得到一个奇怪的数字。只要你在过渡期间不需要知道该部分,你就可以了。
我希望这就是您想要的,让您的类实现UIScrollViewDelegate,并实现以下函数。 它将确保您仅在tableview停止滚动时检索节

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate)
{
        // retrieve section here
        section = [[[self.tableview visibleCells] objectAtIndex:0] section];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
        // retrieve section here
        section = [[[self.tableview visibleCells] objectAtIndex:0] section];
}

我认为,与其查询VisibleCell,不如通过以下方式查询索引路径:

NSInteger section = [[[self.tableView indexPathsForVisibleRows] firstObject] section];

您可以根据需要在一种
UIScrollViewDelegate
方法中使用此代码。

Swift版本

使用第一个(当前锁定)或最后一个(最后显示)


我想通过这样做,它可以防止部分在转换时出现混淆的问题?由于某种原因,这会在我的自定义单元格中抛出错误…`-[AppointmentCustomCell节]:发送到实例0x89af850`的选择器无法识别。我所做的就是在您提供的上述代码中记录该部分。这很旧,但有各种错误,因为
self.tableView visibleCells
提供了
uitableviewcell
而不是
IndexPaths
。获取indexPaths后,我们可以执行indexPaths[0]。section。我应该使用什么方法来保持sectionNumber最新?Scrollviewdidscroll如果希望它在每次用户滚动时更新,则必须在访问其第0个元素之前检查
[tableView visibleCells].count>0