[英]UITableViewCell accessoryView not appearing until much later
I've got a UITableView with several entries. 我有一个带有几个条目的UITableView。 Upon selecting one, I need it to do a potentially time-consuming network operation.
选择一个后,我需要它进行可能耗时的网络操作。 To give the user some feedback, I tried to place a UIActivityIndicatorView in the UITableViewCell.
为了给用户一些反馈,我尝试将UIActivityIndicatorView放在UITableViewCell中。 However, the spinner doesn't appear until much later -- after I've done the expensive operation!
但是,微调框直到很久以后才出现-在我完成了昂贵的操作之后! What am I doing wrong?
我究竟做错了什么?
- (NSIndexPath *) tableView:(UITableView *) tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner autorelease];
[spinner startAnimating];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryView:activity];
if ([self lengthyNetworkRequest] == nil) {
// ...
return nil;
}
return indexPath;
}
As you can see, I have the spinner being set to the accessoryView before the lengthy network operation. 如您所见,在进行冗长的网络操作之前,我已将微调器设置为annexView。 But it only appears after the
tableView:willSelectRowAtIndexPath:
method finishes. 但是它仅在
tableView:willSelectRowAtIndexPath:
方法完成后出现。
Once you tell the ActivityIndicator to start animating, you have to give your application's run loop a chance to start the animation before starting the long operation. 告诉ActivityIndicator开始动画处理之后,必须在开始长操作之前给应用程序的运行循环一个启动动画的机会。 This can be achieved by moving the expensive code into its own method and calling:
这可以通过将昂贵的代码移入其自己的方法并调用以下命令来实现:
[self performSelector:@selector(longOperation) withObject:nil afterDelay:0];
EDIT: I think you should be using didSelect instead of willSelect. 编辑:我认为您应该使用didSelect而不是willSelect。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Try adding [CATransaction flush]
right before 尝试在之前添加
[CATransaction flush]
if ([self lengthyNetworkRequest] == nil) {
- (NSIndexPath *) tableView:(UITableView *) tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner autorelease];
[spinner startAnimating];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryView:activity];
if ([self lengthyNetworkRequest] == nil) {
//doing the intensive work after a delay so the UI gets updated first
[self performSelector:@selector(methodThatTakesALongTime) withObject:nil afterDelay:0.25];
//you could also choose "performSelectorInBackground"
}
return indexPath;
}
- (void)methodthatTakesALongTime{
//do intensive work here, pass in indexpath if needed to update the spinner again
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.