我有一个UITableView的问题。我在后台线程中从数据库服务器加载数据,如果完成,它会发送通知。在通知中,我更新了我的视图的数据数组,并在tableview上使用reloadData。 然后tableview取消选择所选的行(这意味着数据被重新加载),如果我想选择另一行,我会在didSelectRowAtIndexPath的第一行获得EXC_BAD_ACCESS,即使它只是一个NSLog。重新加载数据后选择行崩溃
如果我不分配新数组,后台线程会将变量数据提供给我并且不使用reloadData我没有使用didSelectRowAtIndexPath的问题,但tableview不显示最近的记录。用户必须关闭视图并再次打开以查看更改。这真的很糟糕,我想在后台线程完成从服务器加载记录后马上显示这些更改。
在.h文件中声明的变量:
-downloadThread是NSThread,
- 数据是一个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);
}
}
请在通知方法中发布代码? –
那么你在didSelectRowAtIndexPath的第一行记录什么?在cellForRowAtIndexPath中发生了什么?我怀疑这是你的错误所在,可能无法正确回收细胞。 –
请注意,您不应该从后台调用reloadData(这肯定会导致崩溃),但应该使用peformSelectorOnMainThread或其他方法来使调用在UI线程中执行。 –