2012-04-20 46 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