xcode self.tableView reloadData 在 viewWillAppear 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16042209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
self.tableView reloadData not working in viewWillAppear
提问by Stephen Fig Roll Smith
I have an Xcode app that creates a tableview and then populates this with data from an sqlite database, this works perfectly and allows me to push data to a new view and delete etc,
我有一个 Xcode 应用程序,它创建一个 tableview,然后用来自 sqlite 数据库的数据填充它,这完美地工作,并允许我将数据推送到新视图并删除等,
This tableview is the first view loaded on a tab bar controller. The problem arises when i move to another view on the same controller, i.e. create an athlete, in this view i can add a new athlete to the database (which works fine) but when i go back to the tableview on the tab bar controller, the table is the same, i have to log out and log back in to see the change in the table,
此 tableview 是加载到选项卡栏控制器上的第一个视图。当我移动到同一个控制器上的另一个视图时出现问题,即创建一个运动员,在这个视图中我可以将一个新运动员添加到数据库(工作正常)但是当我回到标签栏控制器上的 tableview 时,表是一样的,我必须注销并重新登录才能看到表中的变化,
I have tried [self.tableView reloadData] in the viewWillAppear function but this does nothing. I know that the function is getting called as i have placed an NSLog in their just for peace of mind.
我在 viewWillAppear 函数中尝试过 [self.tableView reloadData] 但这没有任何作用。我知道该函数正在被调用,因为我在他们中放置了一个 NSLog 只是为了让他们安心。
I have found what i think may be the solution on here but i cant get it to work!
我在这里找到了我认为可能的解决方案,但我无法让它工作!
viewWillAppear, viewDidAppear not being called, not firing
viewWillAppear, viewDidAppear 没有被调用,没有触发
i will post my code below and a screen shot of the storyboard as im convinced it is my poor way of setting up controllers that has caused this error. Any help would be much appreciated as i am now at the meltdown stage :D
我将在下面发布我的代码和故事板的屏幕截图,因为我相信这是我设置控制器的糟糕方式导致了这个错误。任何帮助将不胜感激,因为我现在处于崩溃阶段:D
-(void)viewWillAppear:(BOOL)animated {
//[super viewWillAppear:animated];
//[self.tabBarController viewWillAppear:animated];
[self.tableView reloadData];
NSLog(@"i just ran viewwillAppear");
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
athleteObject *athObj3 = [athletes objectAtIndex:indexPath.row];
athleteIdDelete = athObj3.athId;
[self openDatabase];
[self deleteAthleteFromDataBase];
[athletes removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
reloadData works in my commitEditing function (this updates the tableview when i delete an entry via a swipe)
reloadData 在我的 commitEditing 函数中工作(当我通过滑动删除条目时,这会更新 tableview)
any help would be much appreciated, il post the screen shot below.
任何帮助将不胜感激,我会在下面发布屏幕截图。
(cant figure out how to load a screenshot if its even possible)
(如果可能的话,无法弄清楚如何加载屏幕截图)
回答by Pablo
It looks like athletes (a NSMutableArray I assume) is not being updated in viewWillAppear. Have you tried running the selector that populates the array from the database before calling reloadData?
看起来运动员(我假设是 NSMutableArray)没有在 viewWillAppear 中更新。您是否尝试过在调用 reloadData 之前运行从数据库填充数组的选择器?
Hope that helps!
希望有帮助!
回答by jakenberg
I'm assuming you're setting the number of rows/sections in your code based on how many objects are returned from a database.
我假设您根据从数据库返回的对象数量来设置代码中的行数/节数。
Set a breakpoint at this method, and get the count each time the compiler breaks. You are probably not getting any objects from the database, and therefore the compiler never runs the rest of the table view dataSource methods.
在该方法处设置断点,每次编译器中断时获取计数。您可能没有从数据库中获取任何对象,因此编译器永远不会运行其余的表视图 dataSource 方法。