【问题标题】:UITableView contentSize calculated differently in iOS8 and iOS9?iOS8 和 iOS9 中的 UITableView contentSize 计算方式不同?
【发布时间】:2016-02-23 22:49:27
【问题描述】:

我正在使用下面的代码来强制 uitableview “对齐行”:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    // if decelerating, let scrollViewDidEndDecelerating: handle it
    if (decelerate == NO) {
        [self centerTable];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self centerTable];
}

- (void)centerTable {
    NSIndexPath *pathForCenterCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))];

    [self.tableView scrollToRowAtIndexPath:pathForCenterCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

它在 iOS9 中运行良好,但在 iOS8 中,表格视图不允许我捕捉到第一行或最后一行。如果我在索引路径计算之后在centerTable 中放置一个断点,我会为表格视图得到这个:

iOS9:

<UITableView: 0x7fb9a6ed8600; frame = (0 0; 300 132); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7fb9ae14cae0>; layer = <CALayer: 0x7fb9ad1b1540>; contentOffset: {0, 0}; contentSize: {300, 220}>

iOS8:

<UITableView: 0x7ff6e2d59800; frame = (0 0; 300 132); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ff6e8726130>; layer = <CALayer: 0x7ff6e214b450>; contentOffset: {0, 0}; contentSize: {300, 132.5}>

请注意,除了 contentSize 之外,一切都是一样的。这导致与 iOS9 相比,iOS8 中的索引路径为 n+1

那么,为什么在 iOS8 系统中 contentSize 的计算方式不同???解决方法是什么?

【问题讨论】:

标签: ios uitableview ios8 ios9 contentsize


【解决方案1】:

虽然我不明白为什么 contentSize 会有所不同,但这是我针对准确居中问题的解决方法:

- (void)centerTableViewAnimated:(BOOL)animated {
     NSIndexPath *centerCellIndexPath = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))];

     CGPoint contentOffset = CGPointMake(0, (centerCellIndexPath.row * self.tableView.rowHeight) - ((self.tableView.frame.size.height / 2) - (self.tableView.rowHeight / 2)));

     [self.tableView setContentOffset:contentOffset animated:animated];
}

所以基本上,我没有尝试使用scrollToRowAtIndexPath,而是直接设置contentOffset(您可以对其进行动画处理,这很好)。这似乎在 iOS 8 和 iOS 9 上运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 2016-05-14
    • 2014-10-19
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 2015-05-04
    相关资源
    最近更新 更多