gpt4 book ai didi

ios - UITableView 上方的 UIView 未显示——iOS

转载 作者:行者123 更新时间:2023-11-28 20:33:09 24 4
gpt4 key购买 nike

我们在 iOS 上的 tableView 上方显示 View 时遇到问题。我们的方法是创建一个 UIView,它是 UIViewController 子类的 subview ,发送它到后面,然后在 didSelectRowAtIndexPath 上将它带到前面。我们正在使用 XIB 来创建用户界面。 View 层次结构就像这个:

查看
-- UIView(“正在加载...” View )
-- -- UILabel ("加载中...")
-- -- UIActivityIndi​​catorView
-- UITableView
-- 用户界面标签

这是我们尝试显示“正在加载” View 的方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create a request to the server based on the user's selection in the table view
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSError *err;

// Show the "loading..." message in front of all the other views.
[self.view bringViewToFront:self.loadingView];
[self.loadingWheel startAnimating];

// Make the request
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

// Stop animating the activity indicator.
[loadingWheel stopAnimating];

// other stuff...
}

每当我们将“loading” View 放在所有其他 View 的前面时XIB,我们可以看到它看起来像我们想要的。然而,当我们离开装载在后面查看(根据上面的 View 层次结构),然后尝试将其带到前面, View 永远不会显示。打印出 self.view.subviews 表明我们的加载 View 实际上在 View 层次结构中。有趣的是,如果我们尝试在 didSelectRowAtIndexPath 中更改我们 View 中的其他内容(例如,更改已显示在 View 中的标签的背景颜色),更改永远不会显示在模拟器上。

最佳答案

问题是同步请求。它阻塞了主线程,因此事件指示器没有机会显示。

一个简单的解决方案是将数据异步加载到全局队列中,并在所有内容加载完毕后回调主队列。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Make the request
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

dispatch_async(dispatch_get_main_queue(), ^{
// Stop animating the activity indicator.
[loadingWheel stopAnimating];

// other stuff...
});
});

虽然上述解决方案有效,但它会阻塞全局队列,因此并不理想。查看通过 NSURLConnection 进行的异步加载。在 Apple 的“URL 加载系统编程指南”中有非常详细的解释。

关于ios - UITableView 上方的 UIView 未显示——iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11529880/

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