我有一个应用程序的工作方式与 iPhone 的联系人应用程序的工作方式有些相似。当我们添加一个新的联系人时,用户会被定向到一个包含联系人信息的只能查看的屏幕。如果我们从导航栏中选择“所有联系人”,用户将导航到所有联系人列表,其中可以查看最近添加的联系人。
我们可以使用以下方法将 View 移动到特定行:
[itemsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
...但是它不起作用。我在调用后立即调用:
[tableView reloadData];
我想我不应该在这里调用 selectRowAtIndexPath:animated:scrollPosition
方法。但如果不在这里,那么在哪里?
是否有任何委托(delegate)方法在以下方法之后被调用?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
最佳答案
我想我有一个类似的应用程序:项目列表。点击“+”-> 新屏幕,返回并查看更新后的列表,滚动以在底部显示添加的项目。
总而言之,我将 reloadData
放入 viewWillAppear:animated:
并将 scrollToRowAtIndexPath:...
放入 viewDidAppear:animated:
.
// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (dataHasChanged) {
self.dataHasChanged = NO;
[[self tableView] reloadData];
} else {
self.scrollToLast = NO; // No reload -> no need to scroll!
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (scrollToLast) {
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
[[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
希望对您有所帮助。如果您在列表中间添加内容,则可以轻松滚动到该位置。
关于ios - 在 UITableView 中设置滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/413055/