gpt4 book ai didi

objective-c - subview 呈现延迟

转载 作者:行者123 更新时间:2023-12-03 16:48:12 27 4
gpt4 key购买 nike

在我的 UITableViewController 子类中,我想准备表格数据并在 View 出现后重新加载表格(因此表格最初加载为空)。在我的应用程序委托(delegate)中,我有方法来生成和删除事件屏幕。我的问题似乎是事件 View 呈现被延迟,直到进行 reloadData 调用之后。我通过删除 hideActivity 行证明了这一点,果然我的事件 View 与表的重新加载同时出现。这是我的 View Controller 的 viewDidAppear...

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] showActivity];
[self prepare];
[self.tableView reloadData];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] hideActivity];
}

我假设这可能与未重绘中方法的 View 有关,但不记得以前发生过这种情况。有什么想法吗?

...虽然我知道它们有效,但这是我的事件方法。也许我这样做的方式允许某种延迟出现......

- (void)showActivity
{
self.activityView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
self.activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75f];

UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((320.0f / 2.0f) - (37.0f / 2.0f), (480.0f / 2.0f) - (37.0f / 2.0f) - (20.0f / 2.0f), 37.0f, 37.0f)] autorelease];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[spinner startAnimating];

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, spinner.frame.origin.y + spinner.frame.size.height + 10.0f, 320.0f, 30.0f)] autorelease];
label.textAlignment = UITextAlignmentCenter;
label.text = @"Working";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];

[self.activityView addSubview:label];
[self.activityView addSubview:spinner];

[self.window addSubview:self.activityView];
}

- (void)hideActivity
{
[self.activityView removeFromSuperview];
[self.activityView release];
}

最佳答案

在您将控制权返回给运行循环之前,您的 UI 不会更新。您需要在后台线程中执行数据准备,或者将控制权返回给运行循环以在开始准备之前更新 UI,如下所示:

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] showActivity];
[self performSelector:@selector(prepareData) withObject:nil afterDelay:0.0];
}

- (void)prepareData {
[self prepare];
[self.tableView reloadData];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] hideActivity];
}

关于objective-c - subview 呈现延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4683311/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com