当我的UITableView还没有数据时,如何显示占位符图像? [英] How do I display a placeholder image when my UITableView has no data yet?

查看:148
本文介绍了当我的UITableView还没有数据时,如何显示占位符图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,在加载时,会显示一个UITableView,其中包含用户的数据。

I have an application that, on load, displays a UITableView with the user's data in it.

然而,当用户第一次加载应用程序时(在他们创建任何数据之前),我想显示,而不是一张空表,一张背景图片(箭头指向'添加记录'导航栏按钮)。用户添加了第一条记录后,将显示tableview。我见过很多应用程序这样做 - 目前我能想到的唯一例子是Cha-Ching,在你设置任何预算/等之前。但是我不能为我的生活做出如何做到这一点。

However, when the user first loads the application (before they've created any data), I'd like to display, instead of an empty table, a background image (with an arrow pointing to the 'add a record' navbar button). Once the user has added their first record, the tableview is displayed instead. I've seen numerous apps do this - the only example I can think of at present is Cha-Ching, before you have any budgets/etc set up. I can't for the life of me work out how to do this, though.

我最初在主窗口的xib中添加了一个navigationcontroller,其rootViewController是一个自定义viewcontroller / xib。这个rootViewController包含背景图像,上面有一个隐藏的tableview,以及一个管理tableview的自定义tableviewcontroller。这似乎工作正常,如果有数据,它将加载并显示在表中。但是,如果我要在屏幕外滚动数据,应用程序会崩溃,但会出现此错误:

I initially added a navigationcontroller to the main window's xib, the rootViewController of which was a custom viewcontroller/xib. This rootViewController contained the background image with a hidden tableview above it, and a custom tableviewcontroller that managed the tableview. This seemed to work just fine, and if there was data it would load and display in the table. However, if I was to scroll the data offscreen, the app would crash, with this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'*** -[UITextEffectsWindow tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0xd2d130'

我不知道UITextEffectsWindow是什么,或者为什么它试图管理我的tableview。我认为在我的视图层次结构中可能会错误地连接某些内容...

I have no clue what a UITextEffectsWindow is, or why it was trying to manage my tableview. I presume something may be hooked up incorrectly in my view hierarchy...

如果有更简单/更直接的方法,我会非常感激有人可以解释一下。你会怎么做?

If there's a much simpler/more straightforward way of doing this, I'd be very grateful if someone could explain it. How would you do this?

提前致谢。

推荐答案

所以,为了解决这个问题,我在上面的评论中讨论过:

So, to solve this I did as discussed in the comments above:

我创建了一个普通的UIViewController子类,它包含一个UIImageView和一个UITableView。 viewController符合UITableViewDelegate和UITableViewDatasource协议,并负责查看tableView。 viewController类根据数据是否可用简单地显示或隐藏imageView。

I created a normal UIViewController subclass, which contained a UIImageView and a UITableView. The viewController conforms to the UITableViewDelegate and UITableViewDatasource protocols, and looks after the tableView. The viewController class simply shows or hides the imageView depending on whether data is available.

我之前尝试使用UITableViewController管理这两个视图时出错了。一个UITableViewController必须具有的tableView作为其视图,而,这种解决方案,一个的viewController可同时包含所述图像和所述的tableView,并实施管理的tableView必要的协议。

I was going wrong before by trying to manage both these views with a UITableViewController. A UITableViewController has to have a tableView as its view, whereas, with this solution, a viewController can contain both the image and the tableView, and implement the necessary protocols to manage the tableView.

感谢您的帮助!

这篇关于当我的UITableView还没有数据时,如何显示占位符图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆