views:

1126

answers:

2

I have created an up/down arrow segmented control button on the right side of the navigation bar in my detail view. In a table based application, how can I use these up/down arrows to move through cells in the parent table?

The apple "NavBar" sample code has an example of this but the controls are not functional.

The iBird program has this functionality as well and it is very nice. Download the "iBird 15" program for free.

Any thoughts?

Thanks!

A: 

You should be able to do this by calling selectRowAtIndexPath:animated:scrollPosition in the action associated with the segmented control

ennuikiller
How would I call this using - (IBAction)segmentAction:(id)sender:when I can't use indexPath?
Jonah
I am still looking for an answer to this problem. Do you have any more suggestions on how to implement selectRowAtIndexPath:animated:scrollPosition?Thanks!
Jonah
I found this thread with a similar question. I haven't been able to make it work yet, but it seems to be a step in the right directions.http://stackoverflow.com/questions/1188242/hitting-a-next-button-for-a-uitableviewcell
Jonah
+1  A: 

I have got it working with a few small problems. Here is my current code. It's not very elegant but it's working. segmentActionPressed is an NSInteger set in the .h file which I use to specify if the segment control was used to move to the next view.

- (IBAction)segmentAction:(id)sender{
NSIndexPath *selectedRow = CurrentIndexPath; //CurrentIndexPath is an NSIndexPath object that holds the current indexpath.
NSIndexPath *scrollIndexPath;
self.segmentActionPressed = 1;
self.tableView.delegate = self;

if([sender selectedSegmentIndex] == 0)
{
[self.navigationController popViewControllerAnimated:NO];
 if(selectedRow.row == 0) //If on the first row when up button is pressed, send us to the last row.
 {
  scrollIndexPath = [NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section];
  [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section]];
 }
 if(selectedRow.row > 0)  //If on any other row when up button is pressed, go to the next row above.
 {
  scrollIndexPath = [NSIndexPath indexPathForRow:selectedRow.row -1 inSection:selectedRow.section];
  [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:scrollIndexPath.row inSection:selectedRow.section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:scrollIndexPath.row inSection:selectedRow.section]];
 }
}
else if([sender selectedSegmentIndex] == 1)
{
[self.navigationController popViewControllerAnimated:NO];
 if(selectedRow.row == tableDataSource.count -1)  //If on the last row when up down is pressed, go to the first row.
 {
  scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:selectedRow.section];
  [self.tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  [self tableView:self.tableView didSelectRowAtIndexPath:scrollIndexPath];
 }
 if(selectedRow.row < tableDataSource.count -1)  //If on any other row when up down is pressed, go to the next row below.
 {
  scrollIndexPath = [NSIndexPath indexPathForRow:selectedRow.row +1 inSection:selectedRow.section];
  [self.tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  [self tableView:self.tableView didSelectRowAtIndexPath:scrollIndexPath];
 }
}}

In my -didSelectRowAtIndexPath method, I used the following code to move to the next view without animation if the up/down arrows are used.

if (segmentActionPressed == 0)
 {
 [self.navigationController pushViewController:tvc animated:YES];
 }
else if (segmentActionPressed == 1)
 {
 [self.navigationController pushViewController:tvc animated:NO];
 }

Finally, I set the following code to set the segmentActionPressed to "0" when the parent view appears. I also deselect the row here so you can see which row was selected when you move back to the parent view.

- (void)viewWillAppear:(BOOL)animated {
self.segmentActionPressed = 0;
[self.tableView deselectRowAtIndexPath:CurrentIndexPath animated:YES];}

The main problem is that the segment buttons never appear pressed and the action is sent immediately when touched instead of when the button is released as with the "touch up inside" action.

Jonah