ios 7 tableView didSelectRowAtIndexPath shows blan

2019-04-17 06:29发布

问题:

I have some apps designed for iOS 6 and today I went to upgrade them to iOS 7.

They are a simple table view lists and when a record is selected it displays details about that row on another screen. This worked flawlessly in iOS 6. With iOS 7 when I select a row it shows a blank data page on the first selection. If I then go back to the list and reselect the item it then displays the proper data.. After this first blank display it will work fine for any other records..

I am racking my brain and can not for the life of me figure out why this is happening.

Any ideas?

CODE:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {




Statutes *statute = (Statutes *)[self.statutes objectAtIndex:indexPath.row];

if(self.detailView == nil) {
    DetailViewController *viewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    self.detailView = viewController;
    [viewController release];
}

[self.navigationController pushViewController:self.detailView animated:YES];
self.detailView.title = [statute myStatute];
[self.detailView.statuteFullText setText:[statute myDescription]];
[self.detailView.statuteTitle setText:[statute myTitle]];
[self.detailView.favoritesID setText:[statute myPriority]];
[self.detailView.recordID setText:[statute myRecord]];



if ([@"1" isEqualToString:[statute myPriority]]) {

    [self.detailView.favoriteControl setTitle:@"Remove from favorites"];
} else {
    [self.detailView.favoriteControl setTitle:@"Add to favorites"];        
}
}

Also, the part that changes the button title Add to Favorites etc, does not change on the first item selection, it simply shows "Item".

Thanks.

UPDATE:

So I narrowed it down to this piece of code:

    if(self.detailView == nil) {
    DetailViewController *viewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    self.detailView = viewController;
    [viewController release];
}

If I remove the if clause then it happens on every selection. If I comment out the whole code I cant make any selections. So I imaging I have to find another way to initialize my view controller. Any ideas? Why did this work in iOS 6 then?

回答1:

NEW ANSWER: (9/25/13)

I was bothered by the backwards hack that I had to do to get this to work, plus when I called the detailView from other screens I ran into a similar problem that was not fixed by this hack so I found another easier fix..

All I had to do was surround the commands that were used to set the values on the view in question within a asynchronous tag and viola! Everything worked..

Here is my solution:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

Statutes *statute = (Statutes *)[self.statutes objectAtIndex:indexPath.row];

if(self.detailView == nil) {
    DetailViewController *viewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    self.detailView = viewController;
    [viewController release];
}

[self.navigationController pushViewController:self.detailView animated:YES];
dispatch_async(dispatch_get_main_queue(), ^{

self.detailView.title = [statute myStatute];
[self.detailView.statuteFullText setText:[statute myDescription]];
[self.detailView.statuteTitle setText:[statute myTitle]];
[self.detailView.favoritesID setText:[statute myPriority]];
[self.detailView.recordID setText:[statute myRecord]];



if ([@"1" isEqualToString:[statute myPriority]]) {

    [self.detailView.favoriteControl setTitle:@"Remove from favorites"];
} else {
    [self.detailView.favoriteControl setTitle:@"Add to favorites"];        
}

});
}

So now as u can see the

    dispatch_async(dispatch_get_main_queue(), ^{

is around the items to be set on the view. I got rid of all of the extra crap in the -ViewDidLoad section...

perfect.

OLD ANSWER: (9/24/13)

Well, I got it to work. It is a total backwards hack but I'm results driven so I don't care. It does kind of upset me that this functionality has changed and needed this hack but whatever.. I hope this helps others who I am sure may be running into this.

I left the above code alone and in the

- (void)viewDidLoad

section I added the following code:

    DetailViewController *viewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
self.detailView = viewController;
[viewController release];
[self.navigationController pushViewController:self.detailView animated:YES];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.navigationController popToRootViewControllerAnimated:NO];

});

What this does is instantiates the DetailViewController, displays it, then pops it. When the app loads it appears to be at the main screen, as expected, and when making a selection it now works on the first time because technically it is NOT the fist time...

-LK