didSelectRowAtIndexPath不显示视图

时间:2009-09-01 12:01:30

标签: iphone class uiview

我有一个tableView,当用户选择其中一个单元格时,即可加载一个大图像。

此加载需要10秒钟,我想显示带有旋转图标的小视图。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    loadingView = [[LoadingHUDView alloc] initWithTitle: NSLocalizedString(@"Loading image",@"")];
    [self.view addSubview:loadingView];
    [loadingView startAnimating];
    loadingView.center = CGPointMake(self.view.bounds.size.width/2, 150);
    [imageView loadImage: path];
    [loadingView removeFromSuperview];
}

问题是视图(loadingView)从未显示过。似乎对loadImage的调用阻止了它的显示。我可以强制显示该视图吗?

1 个答案:

答案 0 :(得分:2)

问题是图像的加载会占用线程,因此不会使用旋转图标更新视图。

你需要使用不同的线程,虽然它仍然变得复杂,因为你无法从后台线程轻松更新视图!

所以你真正需要做的就是在后台线程中开始加载大图像。

将代码加载到另一个方法中,然后在后台线程上运行它,如下所示:

[self performSelectorInBackground:(@selector(loadBigImage)) withObject:nil];

请记住,在你的-loadBigImage方法中,你需要声明一个NSAutorelease池:

-(void)loadBigImage {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //Code to load big image up
    [pool drain];
}

当它在后台运行时,您的动画加载图标会显示正常。

希望有所帮助

相关问题