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

查看:114
本文介绍了在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;


推荐答案

我想我有一个类似的应用程序:A

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天全站免登陆