【问题标题】:UIView and UITableView, reloadData weird latencyUIView 和 UITableView,reloadData 奇怪的延迟
【发布时间】: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


【解决方案1】:

我不确定是什么导致了您的延迟,但闻起来可能是线程问题。许多 UIKit 类不是线程安全的。作为一般规则,您应该尝试确保所有 UI 交互都发生在主线程上。在您最初发布的代码中,看起来您正在从后台线程调用 reloadData,这似乎有风险。我不清楚从现在开始调用哪个线程,因为您已经更改了代码。

检查您的 doUpdate 代码路径,并确保通过 performSelectorOnMainThread: 完成对您的委托的任何可能导致 UI 更新的回调。

【讨论】:

  • 非常感谢,你让我开心!我检查了这个并最终改变了: - (void)loadingResourceDidFinishPostAction { [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; [self performSelectorOnMainThread:@selector(hideMuar) withObject:nil waitUntilDone:YES]; }
  • 我遇到了同样的问题,但我在 ViewController 中调用了 [self.tableView reloadData]!我将此行更改为 [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];并且没有任何延迟了。我太新手了,无法解释原因,但它解决了问题!谢谢你们!
【解决方案2】:

这是我对您编辑的问题的新答案。

我不知道究竟会发生什么,因为通常情况下,表格视图会重新加载数据并在单独的线程中重新绘制表格视图,这样运行速度不会那么慢。以下是您可以检查的一些猜测:

1/ 检查您是否按单元格标识符正确重用单元格

2/ 检查是否有任何其他数据源和委托方法执行繁重的网络工作,如

* – tableView:cellForRowAtIndexPath:  required method
* – numberOfSectionsInTableView:
* – tableView:numberOfRowsInSection:  required method

或在:

#  – tableView:heightForRowAtIndexPath:
# – tableView:indentationLevelForRowAtIndexPath:
# – tableView:willDisplayCell:forRowAtIndexPath:

【讨论】:

  • 嗨,我已经尝试过这个并且也玩过 [muar setHidden:YES] 但它不起作用
  • 我没有看到您对 [self.tableView reloadData] 的调用?
猜你喜欢
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 2014-07-31
  • 2023-03-20
相关资源
最近更新 更多