ios 7 tableView didSelectRowAtIndexPath在第一次选择时显示空白数据 [英] ios 7 tableView didSelectRowAtIndexPath shows blank data on first selection
问题描述
我有一些专为iOS 6设计的应用程序,今天我将它们升级到iOS 7。
I have some apps designed for iOS 6 and today I went to upgrade them to iOS 7.
它们是一个简单的表格视图列表,当选择一条记录时它在另一个屏幕上显示该行的详细信息。这在iOS 6中完美运行。使用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.
有什么想法吗?
代码:
- (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".
谢谢。
更新:
所以我把它缩小到这段代码:
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子句,则每次选择都会发生。如果我注释掉整个代码,我就无法做出任何选择。所以我成像我必须找到另一种方法来初始化我的视图控制器。有任何想法吗?为什么这在iOS 6中有效呢?
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?
推荐答案
新答案:(9/25/13)
NEW ANSWER: (9/25/13)
我为了让它工作而必须做的向后黑客感到困扰,当我从其他屏幕调用detailView时遇到了类似的问题,这个问题没有被这个黑客修复,所以我发现了另一个更容易修复的问题。
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..
这是我的解决方案:
- (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"];
}
});
}
现在你可以看到
dispatch_async(dispatch_get_main_queue(), ^{
是在视图上设置的项目。我摆脱了-ViewDidLoad部分中的所有额外垃圾...
is around the items to be set on the view. I got rid of all of the extra crap in the -ViewDidLoad section...
完美。
老答案:(2013年9月24日)
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.
我将上述代码单独留在
- (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];
});
这样做是实例化DetailViewController,显示它,然后弹出它。当应用程序加载时,它似乎在主屏幕上,正如预期的那样,并且在进行选择时它现在第一次起作用,因为从技术上讲它不是第一次......
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
这篇关于ios 7 tableView didSelectRowAtIndexPath在第一次选择时显示空白数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!