重载数据后选择行崩溃

时间:2012-11-07 12:56:49

标签: ios uitableview exc-bad-access reloaddata

我遇到了UITableView的问题。我从后台线程中的数据库服务器加载数据,如果完成,它会发送通知。在通知中,我更新了我的视图的数据数组,并在tableview上使用reloadData。 然后tableview取消选择所选行(这意味着重新加载数据),如果我想选择另一行,我会在didSelectRowAtIndexPath的第一行获得EXC_BAD_ACCESS,即使它只是一个NSLog。

如果我没有分配新数组,后台线程会给我变量数据而不使用reloadData我没有使用didSelectRowAtIndexPath但是tableview没有显示最近的记录。用户必须关闭视图并再次打开以查看更改。这真的很糟糕,我想在后台线程完成后从服务器加载记录后立即显示更改。

.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);
    }
}

1 个答案:

答案 0 :(得分:0)

我怀疑teilnehmerLoad在你的后台线程中被调用,并且它正在调用reloadData,这是一个禁忌。

更改它(和/或KundenUpdated)以使用performSelectorOnMainThread来执行reloadData。

确保您没有从后台线程执行任何其他UI操作。

相关问题