【发布时间】:2011-04-06 15:09:46
【问题描述】:
我的视图控制器重绘有些奇怪。视图控制器包含一个 UITableView 和一个微调器。
我有一个 updateFeed 函数(由 IBOutlet 触发),它将 spinner 放在我的视图控制器前面并放置一个 doUpdate 函数进入 NSOperationQueue。
- (void)updateFeed {
[self showMuar];
ResourceLoader *loader = [ResourceLoader sharedResourceLoader];
[loader updateFeed:[self feed] forDelegate:self];
isReloading = YES;
}
- (id<ResourceToken>)updateFeed:(Feed *)feed forDelegate:(id)delegate {
return [self queueOperation:[[Operation alloc] initWithObject:feed withSelector:@selector(doUpdate) delegate:delegate]];
}
doUpdate 函数调用 parser (startParserWithAdd:) 函数,该函数从 Internet 检索内容并将此内容格式化为 UITableView,然后执行 [tableView reloadData ].
- (NSError *)doUpdate {
[self startParserWithAddThreaded:NO];
return nil;
}
- (void)startParserWithAddThreaded:(BOOL)add {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self startParserWithAdd:add];
[pool release];
}
...解析器函数中的某处(startParserWithAdd:)
[tableView setDataSource:self];
[tableView reloadData];
一旦 doUpdate 没有错误地完成并且感谢我的 Operation 类,loadingResourceDidFinish 在我的视图控制器中执行。 loadingResourceDidFinish 在后台发送 spinner。
- (void)loadingResourceDidFinish:(Feed*)f {
isReloading = NO;
[self loadingResourceDidFinishPostAction];
}
- (void)loadingResourceDidFinishPostAction {
[self hideMuar];
}
- (void)hideMuar {
[loadingIndicator stopAnimating];
[self.view sendSubviewToBack:muar];
}
一切正常,但微调器在 loadingResourceDidFinish 完成后大约 4 秒被发送到后台。
确切地说,muar 是一个包含微调器的不透明 UIView。我只是想在我最初的解释中抽象出复杂性!
我尝试对此进行 NSLog 记录,听起来在 reloadData 完成之前没有重新绘制任何内容。我认为这是一个重绘问题,因为如果我在 loadingResourceDidFinish 结束后将 iPhone 转为横向,一切都很好,微调器消失了。
我尝试在 parser 函数中删除 NSAutoreleasePool 但没有改变。
我肯定误解了一些东西,但我不知道它是什么......
编辑
我更改了代码以减少混乱。 UITableView 的 reloadData 现在放在这里:
- (void)loadingResourceDidFinishPostAction {
[tableView reloadData];
[self hideMuar];
NSLog(@"loadingResourceDidFinishPostAction end");
}
对于调试:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath %i", indexPath.row);
...
那么为什么第一个单元格在 loadingResourceDidFinishPostAction 结束后仅 5 秒才重新绘制?
2010-09-02 10:15:35.279 myapp[9632:5b03] loadingResourceDidFinishPostAction end
2010-09-02 10:15:40.208 myapp[9632:5b03] cellForRowAtIndexPath 0
2010-09-02 10:15:40.697 myapp[9632:5b03] cellForRowAtIndexPath 1
2010-09-02 10:15:40.878 myapp[9632:5b03] cellForRowAtIndexPath 2
我的 iPhone 3G 和 iPhone 4 模拟器也是如此。
【问题讨论】:
-
贴一些关于 loadingResourceDidFinish 和 doUpdate 的代码
-
请提供loadingResourceDidFinish的代码。
-
我做了一个重要的编辑并精确了我的查询:为什么第一个单元格在 loadingResourceDidFinishPostAction 结束后仅重绘 5 秒?
标签: iphone uitableview uiviewcontroller