我使用以下代码创建UITableView数据源数组:
-(void) viewWillAppear:(BOOL)animated
{
// IBOutlet of tableview is 'editMsgTableView'
editMsgTableView.dataSource=nil;
editMsgTableView.delegate=nil;
menuMessageArray = [[NSMutableArray alloc] init];
editMainMenuMsgArray = [[NSMutableArray alloc] init];
menuMessageArray = [DBManager fetchmenu:0 noOfRows:32];
for(int i=0; i< [menuMessageArray count]; i++)
{
NSMutableDictionary *menuMsgListDic = [[NSMutableDictionary alloc] init];
menuMsgListDic = [menuMessageArray objectAtIndex:i];
[editMainMenuMsgArray addObject:menuMsgListDic];
}
editMsgTableView.dataSource=self;
editMsgTableView.delegate=self;
[editMsgTableView reloadData];
}
但这是第一次有效。但每当我做一些tableView编辑的东西或来自另一个视图控制器,之后如果调用viewWillAppear
,则reloadData
不起作用。我也尝试过:
dispatch_sync(dispatch_get_main_queue(), ^{
[editMsgTableView reloadData];
});
但没有工作。请帮帮我。
答案 0 :(得分:3)
编辑开始时调用[tableView startUpading];
,编辑完成后,请致电[tableView stopUpdating];
,然后[tableView reloadData];
。
答案 1 :(得分:0)
尝试,
dispatch_async(dispatch_get_main_queue(), ^{
[editMsgTableView reloadData];
});
答案 2 :(得分:0)
你正在给viewController正确的tableViewDelegates吗? :
.h文件中的 @interface theNameOfTheViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
?
我建议不要这样做:
editMsgTableView.dataSource=nil;
editMsgTableView.delegate=nil;
editMsgTableView.dataSource=self;
editMsgTableView.delegate=self;
就是这样:
[self.editMsgTableView reloadData];
确保在使用reloadData时调用所有委托方法(在每个委托方法中放置一个断点,用于在进行更改或视图出现时重新加载表的数据),请记住reloadData方法,调用tableViewController的所有委托方法(如果你正确地向视图控制器提供我之前说过的委托)。如果这对你没有任何帮助,请告诉我。
答案 3 :(得分:0)
我假设您要将视图从一个视图导航到另一个视图,然后您应该在[tableView reloadData]
上调用ViewDidLoad
。
这是因为视图生命周期 - 当您从一个视图返回并更新tableView
数据源时,视图生命周期将如下所示:
viewDidLoad
tableViewDelegateMethods
viewDidAppear
因此,当您更新tableView
的数据源对象并在reloadData
期间致电viewDidLoad
将解决问题。