gpt4 book ai didi

uitableview - 将 UITableView 滚动到不可见的行解决方法会中断 iOS 4.2 GM 种子

转载 作者:行者123 更新时间:2023-11-29 11:24:25 26 4
gpt4 key购买 nike

场景: - 我在弹出窗口中有一个普通的 tableView,它将其高度限制为四个可见行。当弹出 View Controller 被初始化时,表格数据源数组可能被填充,例如,10 个元素。 - 我的用户之前选择了核心数据对象中保留的 10 个元素值之一。 - 初始化弹出 View Controller 时,我将用户选择的核心数据值与数据源数组值相匹配,并将索引保​​存到名为 selectedIndexPath 的 NSIndexPath viewController 属性的行属性中。 - 然后当表格加载时,我选中 selectedIndexPath.row 处的单元格并将其滚动到 UITableViewScrollPositionTop。

历史:我从来没有能够通过 viewController viewWillAppear: 或 viewDidLoad 方法获得 tableView:scrollToRowAtIndexPath:atScrollPosition:animated: 或 scrollRectToVisible:animated: 来完成此操作。经过大量搜索和试验后,我找到了一种可行的解决方法。我从 viewController viewWillAppear 调用了这个方法:

(void)updateContentOffsetForTableView:(UITableView *)tbl withIndexPath:(NSIndexPath *)indexPath {

// Get rect of selected row from which to calculate origin point
CGRect rowRect = [tbl rectForRowAtIndexPath:indexPath];

// Get point of origin of selected row rect
CGPoint ptOffset = CGPointMake(rowRect.origin.x, rowRect.origin.y);

// Set tableView superclass scrollView.contentOffset property with selected row origin
tbl.contentOffset = ptOffset;

// Note: the cell checkmarking is done in tableView:cellForRowAtIndexPath:

传递 tableView 和 selectedIndexPath 属性。再次,这按预期工作:例如,数据源数组中的第 10 个元素将与核心数据值匹配,索引保存到 selectedIndexPath.row,并使用 tableView 和 selectedIndexPath 行调用上述方法,tableView 将出现滚动以包含选中标记的行。

问题:在 4.2 GM 版本中,所需的滚动不再有效 - 数据源数组中的前四个值仅显示四个 tableView 单元格。例如,第 10 行中的单元格仍将像以前一样被勾选,但无法将下方行滚动到 View 中。

请注意,以前的解决方法仅在从 viewWillAppear: 而不是从 viewDidLoad 调用时有效。随着 4.2 GM 种子的发布,上述解决方法不再有效。

这是在 iPad 应用程序中。我正在使用最近发布的 Xcode 3.2.5 和 iOS SDK 4.2 GM seed (Snow Leopard)。我已经验证,当我为 iOS SDK 3.2 模拟器和设备构建时,上面的代码有效。在为 iOS SDK 4.2 GM 模拟器和设备构建时,以上代码、tableView:scrollToRowAtIndexPath:atScrollPosition:animated: 或 scrollRectToVisible:animated: 均无法实现将所需行滚动到 View 中。

这是一个错误吗?任何帮助或想法将不胜感激。

最佳答案

Apple 开发者论坛用户的建议提供了一个可行的解决方案:

这适用于 4.2 GM 种子、模拟器和设备,不再依赖于 UIScrollView.contentOffset hack。

在 View Controller 中 viewWillAppear: 方法:

(void)viewWillAppear:(BOOL)animated {
if (self.savedIndexPath) {
[self performSelector:@selector(displayRowAtSelectedIndexPath:) withObject:self.savedIndexPath afterDelay:0.0];
}

调用:

(void)displayRowAtSelectedIndexPath:(NSIndexPath *)indexPath {

if (indexPath == self.savedIndexPath) {
[self.myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

关于uitableview - 将 UITableView 滚动到不可见的行解决方法会中断 iOS 4.2 GM 种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4100311/

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