2009-09-01 34 views
3

我有一個tableView和當用戶選擇其中一個單元格,即時加載一個大圖像。didSelectRowAtIndexPath不顯示視圖

這個加載需要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的調用阻止它被顯示。我可以強制顯示視圖嗎?

回答

2

問題在於圖像的加載將線程綁定在一起,因此視圖不會與旋轉圖標一起更新。

您需要使用不同的線程,但它仍然會變得複雜,因爲您無法輕鬆更新後臺線程中的視圖!

所以你真正需要做的是啓動在後臺線程加載大圖像。

把代碼的大圖像加載到另一種方法,然後在後臺線程運行,如下所示:

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

記住,你的-loadBigImage方法裏面,你需要聲明一個NSAutorelease池:

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

當這個在後臺運行時,你的動畫加載圖標會顯示出來很好。

希望有幫助

+0

工作就像一個魅力!謝謝!! – Jorge 2009-09-01 12:30:14

+0

沒問題,歡迎來到StackOverflow! – h4xxr 2009-09-01 12:55:47