在 UITableView 中设置滚动位置 [英] Setting scroll position in UITableView

查看:33
本文介绍了在 UITableView 中设置滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它的工作方式与 iPhone 的联系人应用程序的工作方式有些相似.当我们添加一个新的联系人时,用户会被定向到一个包含联系人信息的仅查看屏幕.如果我们从导航栏中选择所有联系人",用户将被导航到最近添加的联系人所在的所有联系人列表.

I have an application that works some what similar to how iPhone's Contact application works. When we add a new Contact user is directed to a view only screen with Contact information. If we select "All Contacts" from the navigation bar, user is navigated to list of all contacts where the recently added contact is in view.

我们可以使用以下方法将视图移动到特定行:

We can move the view to a particular row using:

    [itemsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];

...但它不起作用.我在打电话后马上打电话给:

... but it's not working. I'm calling this right after calling:

    [tableView reloadData];

我想我不应该在这里调用 selectRowAtIndexPath:animated:scrollPosition 方法.但如果不在这里,那么在哪里?

I think I'm not suppose to call selectRowAtIndexPath:animated:scrollPosition method here. But if not here, then where?

是否有在以下方法之后调用的委托方法?

Is there any delegate method that gets called after the following method?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

推荐答案

我想我有一个类似的应用程序:项目列表.,点击+" -> 新屏幕,回来看看更新的列表,滚动以在底部显示添加的项目.

I think I've got a similar app: A list of item., tap '+' -> new screen, come back and see the updated list, scrolled to show the added item at the bottom.

总而言之,我把 reloadData 放在 viewWillAppear:animated:scrollToRowAtIndexPath:... 放在 viewDidAppear:animated:.

In summary, I put reloadData in viewWillAppear:animated: and scrollToRowAtIndexPath:... in 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];
    }
}

我希望这会有所帮助.如果您在列表中间添加一些内容,则可以轻松地滚动到该位置.

I hope this helps. If you add something in the middle of the list, you could easily scroll to that position instead.

这篇关于在 UITableView 中设置滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆