2011-06-14 15 views
3

我可以成功地推動我的iPhone應用程序只有下一個視圖。但是,導致下一個視圖檢索數據以填充UITableViews,有時等待時間可能會持續幾秒或稍長一些,具體取決於連接。活動時推動下一個視圖指標 - didSelectRowAtIndexPath

在此期間,用戶可能會認爲應用程序已凍結等。因此,爲了解決此問題,我認爲實施UIActivityIndicators是讓用戶知道該應用程序正在工作的好方法。

有人可以告訴我在哪裏可以實現嗎?

謝謝。

pushDetailView方法

- (void)pushDetailView { 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
//load the clicked cell. 
DetailsImageCell *cell = (DetailsImageCell *)[tableView cellForRowAtIndexPath:indexPath]; 

//init the controller. 
AlertsDetailsView *controller = nil; 
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 
    controller = [[AlertsDetailsView alloc] initWithNibName:@"DetailsView_iPad" bundle:nil]; 
} else { 
    controller = [[AlertsDetailsView alloc] initWithNibName:@"DetailsView" bundle:nil]; 
} 

//set the ID and call JSON in the controller. 
[controller setID:[cell getID]]; 

//show the view. 
[self.navigationController pushViewController:controller animated:YES]; 

回答

6

可以在didSelectRowAtIndexPath:方法本身實現這一點。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge]; 
cell.accessoryView = spinner; 
[spinner startAnimating]; 
[spinner release]; 

這將顯示在單元格右側的活動指示器,它會讓用戶感覺到某物正在加載。

編輯:如果設置accessoryView的譜寫相同的方法加載代碼,用戶界面將得到更新,只有當所有操作結束。解決這個問題的方法是在didSelectRowAtIndexPath:方法中設置activityIndi​​cator,並調用視圖控制器延遲推送代碼。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge]; 
    cell.accessoryView = spinner; 
    [spinner startAnimating]; 
    [spinner release]; 

    [self performSelector:@selector(pushDetailView:) withObject:tableView afterDelay:0.1]; 
} 

- (void)pushDetailView:(UITableView *)tableView { 

    // Push the detail view here 
} 
+0

+1:好方法 – Jhaliya 2011-06-14 11:13:32

+0

@Simon:酷,我會試試。有一個問題,我曾經有過幾次這樣做,但是有幾次,活動指示器顯示了視圖被推動時的情況。上面的這個方法是否會在用戶點擊單元格時顯示活動指示符?謝謝。 – 2011-06-14 11:16:24

+0

K.Honda,查看我的更新回答 – EmptyStack 2011-06-14 11:45:16