ios - 在 UITableView 中设置滚动位置

标签 ios objective-c cocoa-touch uitableview

我有一个应用程序的工作方式与 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/

相关文章:

iphone - iphone 上 ScrollView 缩放图像?

ios - UIButton 图像不显示在导航栏中

ios - 截取多个 View - Objective-C

ios - IOS Ipad mini 中的选择框选择问题

html - 如何在 Swift 中动态加载 HTML

javascript - 仅通过网页区分 iPhone 型号?

ios - iOS NSUrlConnection检查上传速度

objective-c - 如何在 Objective-C 中将范围转换为 NSArray

iphone - 适用于iPhone的AMR编码器

ios - 如何检查 ScrollView 是否在底部滚动或不在 ios 中