I am implementing an a UITableView and have the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method implemented and within that method I have if loops (below):
NSInteger row = [indexPath row];
if (self.someDetailViewController == nil) {
if (row==0) {
OneTableViewController *aDetail = [[OneTableViewController alloc] initWithNibName:@"OneDetail" bundle:nil];
self.oneDetailViewController = aDetail;
[aDetail release];
} else if (row==1) {
OneTableViewController *aDetail = [[OneTableViewController alloc] initWithNibName:@"TwoDetail" bundle:nil];
self.oneDetailViewController = aDetail;
[aDetail release];
}
}
However each time I select something in my table view (let's say row 0) I am taken to the secondary view (OneDetail) and then when I go back and select another row (row 1) and I expect to go to the other view (TwoDetail) however I am taken to OneDetail (to original row that was selected first) - how can this be when the user taps another row they are taken to the first row's secondary view that was originally tapped. This also happens vice versa (i.e. selecting row 1 and being taken to TwoDetail开发者_开发技巧 and then going back and selecting row 0 and also being taken to TwoDetail not OneDetail...
I was wondering if anyone knew how to 'restart' an if loop when the user presses the back button or how to overcome my issue in some other fashion. Thanks so much in advance!
I suspect that the second time you enter the method, this check is returning false:
if (self.someDetailViewController == nil)
Thus, you never get into the part where you check which row you're on, and you're permanently stuck with whichever one you set first.
This is not a loop.
You store your detail view controller in the instance variable oneDetailViewController
.
Only if that someDetailViewController
is nil, which is the case most probably only when the method is executed for the first time, you will assign a value.
A view controller that is initialized with "oneDetail".
Unfortunately you do not show the remaining code to us. I assume that you do not have a statement
self.someDetailViewController = nil;
further down in the method.
Why do yo do that == nil thing anyway? What is the detail view controller good for in the further processing? Just release it at the end of didSelectRowAtIndexPath
and create a new one next time it is executed. There is no need for an instance variable saving it.
Hoever, if you have good reasons for this unusual piece of code, which you did not share with us, then we could certainly make a suggestion on how to achieve that - if you let us know :)
精彩评论