从 appDelegate 调用数据时重新加载 tableview 源
我有一个基于表的应用程序,将数据存储在 mysql 服务器上,它会更新并将数据写入 nsdictionary 以实现持久性。当您更改数据时,表需要更新。但是,如果我放置 [self tableview reloadData],则应用程序在选择行时会崩溃。有谁知道如何使表刷新发生。我是否需要放弃这个并从数据中创建一个对象并使用它?
谢谢。
-(void) loadData3;{
MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:@"Rows"];
NSLog(@"AppDelegate.data3 : %@",AppDelegate.data3 );
NSLog(@"self.tableDataSource3 : %@",self.tableDataSource3 );
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData3];
if(CurrentLevel3 == 0) {
self.navigationItem.title = @"Family";
}
else
self.navigationItem.title = CurrentTitle3;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
这是 cellForRowAtIndexPath 方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSDictionary *dictionary = [self.tableDataSource3 objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"Title"];
return cell;
}
I have a table based app that stores data on a mysql server, it gets updated and writes the data to nsdictionary for persistance. When you make a change to the data, the table need to update. However if I put a [self tableview reloadData] the app crashes when selecting a row. Does anyone have an Idea on how to make the table refresh happen. Do I need to ditch this and make an object from the data and use that?
Thanks.
-(void) loadData3;{
MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:@"Rows"];
NSLog(@"AppDelegate.data3 : %@",AppDelegate.data3 );
NSLog(@"self.tableDataSource3 : %@",self.tableDataSource3 );
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData3];
if(CurrentLevel3 == 0) {
self.navigationItem.title = @"Family";
}
else
self.navigationItem.title = CurrentTitle3;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
Here is the cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSDictionary *dictionary = [self.tableDataSource3 objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"Title"];
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意 reloadData 是正确方法的解释,您需要找到并修复导致崩溃的错误。
这里有两个重要的事情需要注意:
确保在更改数据库后,对 numberOfRowsInSection 的调用将立即返回有效的新值作为表被修改的结果。 (我看到很多人因此而发布问题)。
仔细查看您的 cellForRowAtIndexPath 方法。请记住,此方法不仅设置单元格中的数据,还负责回收单元格。我见过很多处理不当的情况 - 当表数据随后重新加载时 - 并且需要回收和重新分配单元格 - 事情就会变得混乱。我敢打赌 99% 的问题都出在此处。
I concur with the explanation that reloadData is the correct way to go, and you need to find and fix the bug that is causing your crash.
Here are two giant things to look at:
Make sure immediately after altering the database, the call to numberOfRowsInSection will return the valid, new value as a result of the table being modified. (I have seen many people post issues due to this).
Look carefully at your cellForRowAtIndexPath method. Remember, this method doesn't only set the data in the cells, but is also responsible for recycling cells. I have seen many instances where this was mishandled - and when table data is subsequently reloaded - and the cells need to be reclaimed and reassigned - things go haywire. I'd bet 99% that your issue is in here.