我有一个 UITableView 的问题。我在后台线程中从数据库服务器加载数据,如果完成,它会发送通知。在通知中,我更新了视图的数据数组并在 tableview 上使用了 reloadData。然后 tableview 取消选择选定的行(这意味着数据被重新加载),如果我想选择另一行,我会在 didSelectRowAtIndexPath 的第一行得到 EXC_BAD_ACCESS,即使它只是一个 NSLog。
如果我不分配新数组,后台线程会给我变量数据并且不使用 reloadData 我对 didSelectRowAtIndexPath 没有问题,但表格视图不显示最近的记录。用户必须关闭视图并再次打开它才能看到更改。这真的很糟糕,我想在后台线程完成从服务器加载记录后立即显示更改。
.h 文件中声明的变量:
-downloadThread 是一个 NSThread,
-data 是一个 NSArray,
-manager 是我的 SQL 接口。
-(void)viewDidLoad
{
...
[super viewDidLoad];
NSMutableArray *arr = [[manager getTeilnehmerList] retain];
data = arr;
[self.tableView reloadData];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KundenUpdated:) name:@"ContactUpdate" object:nil]; // to be notified when updating thread is finished
downloadThread = [[NSThread alloc] initWithTarget:self selector:@selector(teilnehmerLoad:) object:nil]; //Thread to get actual data on Background
[downloadThread performSelector:@selector(start) withObject:nil afterDelay:2];
...
}
-(void)teilnehmerLoad:(id)sender
{
[manager loadTeilnehmerFromServerAndInsertIntoDatabase];
//data = [manager getTeilnehmerList];
[self.tableView reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ContactUpdate" object:nil];
}
-(void)KundenUpdated:(NSNotification*)notifaction
{
@synchronized(self)
{
//needs function to select row that was selected before reload if the data record is still there after sync with server
[self.tableView reloadData];
NSLog(@"count data in kundenupdated: %i",data.count);
}
}