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

问题描述

我有一个RootViewController,它是一个带有UITableView的UIViewController.此RootViewController根据选定的行推送一个ChildViewController.当我弹出ChildViewController并返回到RootViewController时,我希望能够知道所选的indexPath.有了indexPath后,我想调用selectRowAtIndexPath:animated:scrollPosition:方法将所选的indexPath滚动到顶部.

I have a RootViewController that is a UIViewController with a UITableView. This RootViewController pushes a ChildViewController based on the row selected. When I pop the ChildViewController and go back to the RootViewController, I want to be able to know the indexPath that was selected. Once I have the indexPath, I want to call the selectRowAtIndexPath:animated:scrollPosition: method to scroll that selected indexPath to the top.

我知道如何在表视图(indexPathForSelectedRow)中获取选定的indexPath,但是当我按下ChildViewController然后弹出到RootViewController时,我不知道如何保持该值.

I know how to grab the selected indexPath within the tableview (indexPathForSelectedRow) but I don't know how to hold onto that value when I push the ChildViewController and then pop back to the RootViewController.

推荐答案

向表视图控制器.h文件中添加字段:

Add a field to your table view controller .h file:,

    NSIndexPath* selectedIndexPath;

    //...
}

//...

@property (nonatomic, retain) NSIndexPath* selectedIndexPath;

//...

@end

然后添加

@synthesize selectedIndexPath;

在您的.m文件中.之后,使用

in your .m file. After that, store the value using

    self.selectedIndexPath = myIndexPath;

,然后按如下所示使用它:

and then use it as follows:

- (void)viewWillAppear:(BOOL)animated {
        [self.tableView scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}

这篇关于弹出后如何选择UITableView的indexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 10:40