scrollToRowAtIndexPath

scrollToRowAtIndexPath

本文介绍了UITableView scrollToRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的表格视图记住它的最后位置,以便在应用程序再次运行后,它将滚动到该位置。要做到这一点,在我的 viewWillDisappear:方法中,我得到第一个可见的行号并将其保存到 NSUserDefaults 。然后在我的viewDidAppear中,我尝试通过向表视图发送 scrollToRowAtIndexPath 来滚动到该行。但是我得到 NSRangeException

I'm trying to make my table view remember it's last position, so that after the app is run again, it will scroll to that position. to do this, in my viewWillDisappear: method, I get the first visible row number and save it to NSUserDefaults. then in my viewDidAppear I try to scroll to that row by sending scrollToRowAtIndexPath to the table view. However I get a NSRangeException:

任何帮助表示赞赏。以下是代码:

any help appreciated. Here is the code:

- (void)viewDidAppear:(BOOL)animated
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    if ([userDefaults valueForKey:@"content_row"] != nil)
    {
        int rowToHighlight = [[userDefaults valueForKey:@"content_row"] intValue];
        NSIndexPath * ndxPath= [NSIndexPath indexPathForRow:rowToHighlight inSection:0];
        [contentTableView scrollToRowAtIndexPath:ndxPath atScrollPosition:UITableViewScrollPositionTop  animated:YES];
    }
}

-(void) viewWillDisappear:(BOOL)animated
{
    NSIndexPath *selectedRowPath = [[contentTableView indexPathsForVisibleRows] objectAtIndex:0];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setInteger:selectedRowPath.row forKey:@"content_row"];
}


推荐答案

在调用scrollToRowAtIndexPath之前重新加载TableView数据。

Reload TableView data before calling scrollToRowAtIndexPath.

[contentTableView reloadData];

这篇关于UITableView scrollToRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:13