views:

456

answers:

2

Hello all ,

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 )

+1  A: 

Perhaps there's some complexity to your question that I'm missing, but you could simply track the selected row in your code and, when you get the event from the toolbar button, increment your selected row index and use the:

-selectRowAtIndexPath:animated:scrollPosition:

on your table to select the row?

Fraser Speirs
ok super thanks man .. but i forgot to mention that I would like it to act as if it was selected , touched;) thnx for the reply though
If you're implementing -tableView:didSelectCellAtIndexPath: in the normal way, why not just call that method directly? You're computing an `indexPath` for the row anyway, so just call the selection delegate method.
Fraser Speirs
Excuse me, it's `-tableView:didSelectRowAtIndexPath:` not didSelectCellAtIndexPath. Should have double-checked, sorry.
Fraser Speirs
great man I've added this to the bottom and it works .. [self tableview:[self tableView] didSelectRowAtIndexPath:selectedRow];thnx
+1  A: 

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
}
sarnesjo
super thnx man ..unfortunately I forgot to add the part that the row needs to act as if it has been clicked;) .. this code is perfect made something similar but unfortunately it doesn't act like a touch ( for instance if you touched the second row it would get the action behind that cell ).. but thanks a lot anyway
Yes, this is expected behavior. From the documentation of selectRowAtIndexPath:animated:scrollPosition:"Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers."In other words, you'll have to call those methods yourself. I've edited the code above to do so.
sarnesjo
Ok super thnx ... but I've made some adjustments ..all I needed was this one [self tableview:[self tableView] didSelectRowAtIndexPath:selectedRow]; but again thnx