处理空的 UITableView.打印友好信息 [英] Handling an empty UITableView. Print a friendly message
问题描述
我有一个 UITableView,在某些情况下为空是合法的.所以,而不是显示应用程序的背景图像,我更喜欢在屏幕上打印一条友好的消息,例如:
I have a UITableView that in some cases it is legal to be empty. So instead of showing the background image of the app, I would prefer to print a friendly message in the screen, such as:
这个列表现在是空的
最简单的方法是什么?
推荐答案
UITableView 的 backgroundView 属性是你的朋友.
UITableView's backgroundView property is your friend.
在 viewDidLoad
或您 reloadData
的任何地方,您应该确定您的表是否为空,并使用包含 UILabel 的 UIView 更新 UITableView 的 backgroundView 属性或只是设置它为零.就是这样.
In viewDidLoad
or anywhere that you reloadData
you should determine if there your table is empty or not and update the UITableView's backgroundView property with a UIView containing a UILabel or just set it to nil. That's it.
当然可以让 UITableView 的数据源执行双重任务并返回一个特殊的列表为空"单元格,这让我觉得很麻烦.突然 numberOfRowsInSection:(NSInteger)section
必须计算它没有被询问的其他部分的行数,以确保它们也是空的.您还需要制作一个带有空消息的特殊单元格.另外不要忘记,您可能需要更改单元格的高度以容纳空消息.这一切都是可行的,但似乎是创可贴之上的创可贴.
It is of course possible to make UITableView's data source do double duty and return a special "list is empty" cell, it strikes me as a kludge. Suddenly numberOfRowsInSection:(NSInteger)section
has to compute the number of rows of other sections it wasn't asked about to make sure they are empty too. You also need to make a special cell that has the empty message. Also don't forget that you need to probably change the height of your cell to accommodate the empty message. This is all doable but it seems like band-aid on top of band-aid.
这篇关于处理空的 UITableView.打印友好信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!