How to jump to the next row in detailview

2019-03-31 23:20发布

i have a TableView which loads a detailview in didSelectRow. I have no problems with the data's. I am using coreData and to populate the TableView i am using a NSFetchedResultsController.

Now, i want to make a next and previous function in the detailView to jump to the next or previous Table(row) Element. Next Previous like in the Mail-App.
I know that i have to use a button with IBAction.

But how can i make this function?

thanks,
brush51

Edit 1: I have it done so:

- (IBAction) previousCard:(id) sender {  
    NSLog(@"previousBtn");

DashboardViewController *parent = (DashboardViewController *)self.parentViewController;

NSIndexPath *actualIndexPath = selectedRow2;
NSIndexPath * newIndexPath = [NSIndexPath  indexPathForRow:actualIndexPath.row-1 inSection:actualIndexPath.section];
NSLog(@"newIndexPath: %@", newIndexPath);
[parent.tableViews selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; //HERE I GET SIGABRT

}

But i get an error:

-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0'  

How to solve this?

3条回答
等我变得足够好
2楼-- · 2019-03-31 23:37

for that you can manage with your DataSource array.

when you load your detail view at that time you pass your index of array(indexPath.row). by that index you fetch data in detail view. and using increment and decrement in that index you get data for next and previous cell.

Steps

1) setup your tableView.

2) when redirect to DetailView at that time set one int variable for store current indexPath.row . so define one int in detail View.

3) using next and previous button manage indexpath.row which is in int variable. and represent appropriate data from Source array.

查看更多
一夜七次
3楼-- · 2019-03-31 23:43

You can use:

(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

To get the cell you want (next or previous).

And then set the returned cell "Selected" property to YES.

cell.selected = YES

查看更多
放我归山
4楼-- · 2019-04-01 00:01

With your UITableView, you can get the selected row by:

- (NSIndexPath *)indexPathForSelectedRow

And set the selection by incrementing the indexPath.row:

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition

then you action could be:

-(IBAction)nextCell:(id)sender {
  NSIndexPath * actualIndexPath = myTableView.indexPathForSelectedRow;
  NSIndexPath * newIndexPath = [NSIndexPath  indexPathForRow:actualIndexPath.row+1 inSection:actualIndexPath.section];
  [myTableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
查看更多
登录 后发表回答