R – hitting a next button for a UITableViewcell

iteratorobjective cuitableview

I was wondering how I can realize the following:
Creating a next button and when it's tapped the next UITableview row will be selected.

Since a picture is worth a thousand words I've added a screenshot.

http://img529.imageshack.us/img529/4341/picture5uuj.png

As you can see below I added a toolbar and if I press the right button it has to select the next row. Any suggestions on how I can approach this ( I assume with some sort of iterator).

ps. i want the row also to act as if it has been touched ( so i can put an action behind it )

Best Answer

Something like this should do the trick:

- (void)selectNextRow
{
  NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow];
  NSUInteger row = selectedRow.row;
  NSUInteger section = selectedRow.section;
  ++row;
  if(row >= [self.tableView numberOfRowsInSection:selectedRow.section])
  {
    row = 0;
    ++section;
    if(section >= [self.tableView numberOfSections])
    {
      row = 0;
      section = 0;
    }
  }
  NSIndexPath *rowToSelect = [self tableView:self.tableView willSelectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
  [self.tableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionMiddle];
  [self tableView:self.tableView didSelectRowAtIndexPath:rowToSelect];
  // also, post notifications if desired
}
Related Topic