2012-04-20 47 views
1

當我點擊表格單元格時,在加載下一個視圖之前有1-2秒的短暫延遲。我看過一些在那段時間顯示活動指示器的應用程序,這就是我想要做的。我已經添加了一個像這樣的Tablecell click short delay

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

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
    spinner.frame = CGRectMake(200,200,200,200); 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryView = spinner; 
    [spinner startAnimating]; 
    [spinner release]; 

    VenueViewController *vviewcontroller = [[VenueViewController alloc] initWithNibName:@"VenueViewController" bundle:[NSBundle mainBundle]]; 

    [self.navigationController pushViewController:vviewcontroller animated:YES]; 
    [vviewcontroller release]; 
    vviewcontroller = nil;} 

但是,這也會顯示延遲,並在下一個視圖顯示之前。點擊表格單元格後,該應用程序似乎凍結了1-2秒,因此它甚至沒有顯示活動指示器。

回答

2

我認爲祕訣是你應該使用performSelector方法調用load方法。另一個提示是隱藏或顯示活動,因此它不會消耗此操作的時間。

因此,這可能是該

內,您的視圖控制器類定義一個僞代碼:

IBOutlet UIActivityIndicatorView *spin; // created in view and hidden 

在您的實現......

-(void) load{ // your code 
    VenueViewController *vviewcontroller = [[VenueViewController alloc] initWithNibName:@"VenueViewController" bundle:[NSBundle mainBundle]]; 

    [self.navigationController pushViewController:vviewcontroller animated:YES]; 
    [vviewcontroller release]; 
    vviewcontroller = nil; 

    spin.hidden=YES; 
} 

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

    spinner.hidden=NO; 

    [self performSelector:@selector(load) withObject:nil afterDelay:0]; 

} 

希望它能幫助。

+0

爲什麼使用performSelector? – TompaLompa 2012-04-21 14:18:04

+1

因爲它在下一個循環中排隊等待更多信息:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html和http:// developer。 apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject – Williew 2012-04-21 16:17:21

+0

謝謝。 performSelector沒有任何區別,但是我正在iPad 1上進行測試,我認爲這種延遲對於新iPad來說並不是什麼大問題。最後一個問題,我怎樣才能將微調器移動到被點擊的表格單元格內。現在我把它集中在舞臺上。 – user1104325 2012-04-29 05:57:38