gpt4 book ai didi

ios - UITableView contentSize 在 iOS8 和 iOS9 中的计算方式不同?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:08:00 27 4
gpt4 key购买 nike

我正在使用下面的代码来强制 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 中,表格 View 不允许我捕捉到第一行或最后一行。如果我在索引路径计算之后在 centerTable 中放置一个断点,我会为 TableView 得到这个:

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会被系统计算的不一样???解决方法是什么?

最佳答案

虽然我不明白为什么 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 上工作得很好。

关于ios - UITableView contentSize 在 iOS8 和 iOS9 中的计算方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35589811/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com