2011-06-14 45 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