[英]iOS Adding Image to Tableview Background View
I have a tableview that I am using to display data loaded from a web service. 我有一个用于查看从Web服务加载的数据的表视图。 Sometimes it loads fast, other times it take awhile. 有时加载速度很快,而其他时间则需要一段时间。 While it's loading, the tableview just shows a blank screen (in my case it is a gray screen). 加载时,tableview只会显示一个空白屏幕(在我的情况下是灰色屏幕)。 I want to display in the tableview background view a simple image that says loading and has a loading icon that I will animate to spin, but I cannot get it to show up at all. 我想在tableview背景视图中显示一个简单的图像,该图像显示正在加载,并且具有要动画旋转的加载图标,但是我根本无法显示它。 Here is my code: 这是我的代码:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
[backgroundView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 green:227.0 / 255.0 blue:227.0 / 255.0 alpha:1.0]];
self.tableLoading.image = [UIImage imageNamed:@"loading.png"];
self.tableLoading.frame = CGRectMake(145, 80, 30, 30);
[backgroundView addSubview:self.tableLoading];
[self.feedTableView setBackgroundView:backgroundView];
The background view shows up as expected, hence the gray background, but I CANNOT get the image to show up at all. 背景视图按预期方式显示,因此背景为灰色,但我根本无法显示该图像。
Any suggestions? 有什么建议么? This seems like a simple problem but I have already spent a long time at it with no success. 这似乎是一个简单的问题,但我已经花了很长时间没有成功。 Thanks in advance! 提前致谢!
How are you doing your webservice call? 您如何进行网络服务呼叫? I suppose it's done on an asynchronous block. 我想这是在异步块上完成的。 Maybe something like AFNetworking
NSOperation
block? 也许像AFNetworking
NSOperation
块一样? If so, where are you setting the image as a background? 如果是这样,您在哪里将图像设置为背景? All user interface (UI) stuff should not be done on a background thread, it should be done on the main thread since the main thread is the only one who should be involved to UI. 所有用户界面(UI)内容都不应在后台线程上完成,而应在主线程上完成,因为主线程是UI中唯一涉及的线程。
Try the following: 请尝试以下操作:
dispatch_async(dispatch_get_main_queue(), ^{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
[backgroundView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 green:227.0 / 255.0 blue:227.0 / 255.0 alpha:1.0]];
self.tableLoading.image = [UIImage imageNamed:@"loading.png"];
self.tableLoading.frame = CGRectMake(145, 80, 30, 30);
[backgroundView addSubview:self.tableLoading];
[self.feedTableView setBackgroundView:backgroundView];
});
You can start displaying the UITableView
background image when you start the request, and just dismiss it right after the request is finishing networking. 您可以在开始请求时开始显示UITableView
背景图像,并在请求完成联网后立即将其关闭。
For instance, SLRequest
has a sending request method called performRequestWithHandler:
which has a handler to be called when the request is done, within that handler, you can dismiss the custom indicator or remove it from your view controller. 例如, SLRequest
具有一个名为performRequestWithHandler:
的发送请求方法,该方法具有一个在请求完成时要调用的处理程序,在该处理程序内,您可以关闭自定义指示器或将其从视图控制器中删除。
Hope this help. 希望对您有所帮助。
You can avoid using your UIView and instead you can use an activity indicator. 您可以避免使用UIView,而可以使用活动指示器。 Try something like this: 尝试这样的事情:
-(void)viewDidLoad{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
backgroundView.tag=1;
[backgroundView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 green:227.0 / 255.0 blue:227.0 / 255.0 alpha:1.0]];
self.tableLoading.image = [UIImage imageNamed:@"loading.png"];
self.tableLoading.frame = CGRectMake(145, 80, 30, 30);
[backgroundView addSubview:self.tableLoading];
[self.view addSubview:backgroundView];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do your web service call asynchronous
dispatch_sync(dispatch_get_main_queue(), ^{
//All UI needs to be here, in this thread
// remove from superview your loading image to show your tableview content
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 1) {
[subview removeFromSuperview];
}
}
[self.yourTableView reloadData];
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.