Array Indices Messed Up In Navigation Controller Under Tab Bar Controller
So I have an application that embeds a Navigation Controller inside a Tab Bar controller. I have an NSMutableArray in which I get the data.
The problem is when the Table View appears, I click on one, the detail view appears but in a really messed up pattern. If I click Cell 2 for the first time, the Cell 2 detail view shows. If I click Back and click on Cell 2 again, no detail view shows and then Cell 2 Detail View shows u开发者_StackOverflow社区p when I click on Cell 3. and it continues to change orders everytime I click Back on the Navigation. What could I be doing wrong? Here's some code to help:
#pragma mark - View lifecycle methods
- (void)viewDidLoad
{
[super viewDidLoad];
searchResultsArray = [[NSMutableArray alloc] initWithObjects:@"Producer 1", @"Producer 2",@"Producer 3", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [searchResultsArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NearMeDetailViewController *nmdvController = [[NearMeDetailViewController alloc] initWithNibName:@"NearMeDetailViewController" bundle:nil];
NSLog(@"%i", indexPath.row);
nmdvController.producerName = [searchResultsArray objectAtIndex:indexPath.row];
NSLog(@"%@", [searchResultsArray objectAtIndex:indexPath.row]);
[self.navigationController pushViewController:nmdvController animated:YES];
[nmdvController release];
}
You have been added and push it to navigation controller while deselect.
When it will deselect? While selecting other one.
example:
You have first select cell1.ok
then
select cell2.
so now cell1 will first deselect then select cell2.
You were putting your code in deselect. So it will push everytime in next cell or other cell selection.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
not
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
}
精彩评论