UITableView - how can I move straight from one item's view to another?
Part of my app is a notes section, a bit like the iPhone's built-in notes app. The user taps their note in a UITableView (controlled by NSFetchedResultsController), and the UINavigationController shows the note.
At the moment, if the user wants to view another note, they have to go back to the UITableView and choose it there. I would like to have up and down arrows to go straight to the next/previous note.
开发者_Python百科What is the best way of going about this? I'm guessing I need to go back and get the next item from the NSFetchedResultsController somehow without showing the UITableView? And how do I integrate this with the UINavigationController?
I've come up with a rather messy solution. It just about works, but I'm sure there must be something far more elegant and simple. Anyway, here's what I've got working at the moment.
First, the UITableView is a delegate of the note's UIViewController. The note's UIViewController has a property called "editingIndexPath" which is that note's indexPath in the UITableView. When the user presses the "previous note" button, the following method is called:
- (void)backAction:(id)sender
{
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:editingIndexPath.row-1 inSection:editingIndexPath.section];
[self.delegate editingViewControllerDidRequestObjectAtIndexPath:newIndexPath];
}
Then the delegate method in the tableViewController:
- (void)editingViewControllerDidRequestObjectAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController popViewControllerAnimated:NO];
NoteEditViewController *detailViewController = [[NoteEditViewController alloc] init];
[self.navigationController pushViewController:detailViewController animated:animated];
Note *aNote = [self.fetchedResultsController objectAtIndexPath:indexPath];
detailViewController.noteTextView.text = aNote.text;
detailViewController.editingIndexPath = indexPath;
detailViewController.delegate = self;
[detailViewController release];
}
This works, but it's rather inelegant, and always results in a "pop" animation (right-to-left), whether it is going to the next note or the previous one. I have fudged that, too, with some core animation, but I'm sure I'm missing a far simpler, better way of doing this.
It turned out the answer was really simple - I didn't have to change views at all. I just got the new information from the root view controller using a delegate and changed all the properties on the current view controller. One of those properties is an NSIndexPath, so it knows where in the root view's table it appears.
精彩评论