如何在弹出后选择UITableView的indexPath?

时间:2011-04-10 21:44:44

标签: iphone uitableview

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

我知道如何在tableview(indexPathForSelectedRow)中获取所选的indexPath,但是当我按下ChildViewController然后弹回RootViewController时,我不知道如何保持该值。

2 个答案:

答案 0 :(得分:0)

在表视图控制器.h文件中添加一个字段:,

    NSIndexPath* selectedIndexPath;

    //...
}

//...

@property (nonatomic, retain) NSIndexPath* selectedIndexPath;

//...

@end

然后添加

@synthesize selectedIndexPath;
你的.m文件中的

。之后,使用

存储值
    self.selectedIndexPath = myIndexPath;

然后按如下方式使用它:

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

答案 1 :(得分:0)

使用静态变量,它将保存您的Cellselected Indexpath.row值..

对于你的类,

只能在静态方法或类中的.h文件中声明它,否则在@implemented之前

static int tableIndex = nil;

在路径>>>表视图委托方法

中选择行

tableindex = indexpath.row;

  
    

视图会出现..

  

if(tableIndex!= nil){      //做东西 }

由于