从NSOperation重新加载UITableView
问题描述:
我已经编写了下面的代码来从NSInvocationOperation重新加载UITableView。但是,在拨打[tableview reloadData]
后,界面长时间不会更新。从NSOperation重新加载UITableView
Apple文档说在NSOperation中不会调用委托方法。
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(connectToServer)
object:nil];
[queue addOperation:operation];
[operation release];
[queue autorelease];
- (void) connectToServer
{
...
...
[tableview reloadData];
}
答
问题是UI更新必须发生在主线程上,并且reloadData通过NSOperationQueue从后台线程调用。
您可以使用NSObject方法performSelectorOnMainThread:withObject:waitUntilDone:来确保在主线程上发生此类更新。
- (void) connectToServer
{
...
...
[tableView performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO];
}
此外,NSOperationQueue
不应该是一个局部变量。它应该是这个类的一个保留属性,并且只在dealloc
中发布。
我已经尝试过了,它会在输出一段时间后在ios5中输出相同的图片。你能帮忙吗? – nameless 2012-02-15 06:45:09