简体   繁体   中英

Segue back to TableView at selected cell row

I have a UItableView that segues to a different view controller. I'm using a UITapGestureRecognizer that segues back to the tableView. This segue works, but I would like it to segue back to the tableView at the originally selected table row. How do I accomplish this?

Edit: Edited code

- (void)viewWillAppear:(BOOL)animated  {

 appDelegate = [[UIApplication sharedApplication] delegate];
 NSLog(@"%@", NSStringFromCGPoint(appDelegate.testCGPoint));

 [self.tableView setContentOffset:appDelegate.testCGPoint animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

 if ([segue.identifier isEqualToString:@"ShowDetail"]) {

   appDelegate.testCGPoint = [self.tableView contentOffset];

 }
}

You should save the content offset of the table view into a value.
float tableContentOffset;
Then later, save the offset into that float. Finally, when you need to set the content offset of the table view,
[self.tableView setContentOffset:CGPointMake(0,tableContentOffset) animated:YES];

So, in your code, you've created a CGPoint. However, the creation existed within the method prepareForSegue. Thus, you cannot access the point outside of the method! A solution to this is to create the CGPoint in the header file.
CGPoint tableContentOffest;
Then in your prepareForSegue method, all you have to do is SET the table's offset value. Don't create a new CGPoint.
Additionally, in your code [vc.tableView setContentOffset:_tableContentOffset animated:YES]; you would want to set it with [vc.tableView setContentOffset:tableContentOffset animated:YES]; and not _tableContentOffset. Hope this helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM