我正在显示一长串需要用 3 个索引(名称、日期、id)排序的项目。然后我在 self.navigationItem.titleView 中显示带有 UISegmentControl 的列表。用户可以使用该控件来选择他想要显示的排序顺序。然后,用户可以选择根据某些类别过滤该列表。然后我必须再次对列表进行排序,因为使用新的过滤列表可能只会显示一个名称,因此我不想按名称显示排序顺序。所以我需要排序才能重置 UISegmentControl。由于排序需要几秒钟,我将其推送到另一个线程中。这是我的代码:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"value"]) {
//The filter value has changed, so I have to redo the sort
[[self navigationItem] setTitleView:NULL];
[[self navigationItem] setTitle:@""];
self.navigationItem.rightBarButtonItem.enabled = false;
[self.tableView reloadData]; //I reload here because I want to display Loading... on my table (another var has been set before hands so the tableview will display that)
dispatch_async(backgroundQueue, ^(void) {
[self displayAfterFilterChanged];
});
}
}
-(void) displayAfterFilterChanged
{
displayState = DISPLAYRUNNING;
[self setupSort]; //<<< this is where the 3 sort index are setup based on the filter
dispatch_async(dispatch_get_main_queue(), ^(void) { //<<< I call all the UI stuff in the main_queue. (remember that this method has been call from dispatch_async)
[self.tableView reloadData]; //<< the table will display with the sorted and filtered data
self.navigationItem.rightBarButtonItem.enabled = true;
if (!self.leSelecteur) { //<<< if I don't have a UISegmentControl I display a title text
[[self navigationItem] setTitle:@"atitle"];
} else { // I have a UISegmentControl, so I use it.
[[self navigationItem] setTitleView:self.leSelecteur];
NSLog(@"before setneedsdisplay");
[self.navigationItem.titleView setNeedsDisplay];
NSLog(@"after setneedsdisplay");
}
});
}
现在,问题:表格立即重新显示,rightbarbuttonitem 立即启用。但是 navigationItem titleView 需要 5-10 秒才能显示出来。看起来它正在跳过一个显示周期并捕捉下一个。有什么办法可以加快速度吗?我可以看到“before setNeedsdisplay”和“after setneedsdisplay”立即显示在日志上。但实际刷新发生在 5-10 秒后。