如果没有表格视图结果,则显示“无结果”在屏幕上 [英] If no Table View results, display "No Results" on screen
问题描述
我有一个 tableview
,有时可能没有任何结果要列出,所以我想提出一些说没有结果如果没有结果(标签或一个表视图单元格?)。
I have a tableview
, where sometimes there might not be any results to list, so I would like to put something up that says "no results" if there are no results (either a label or one table view cell?).
有最简单的方法吗?
我会在 tableview
后面尝试标签
然后隐藏其中一个这两个基于结果,但因为我正在使用 TableViewController
而不是正常的 ViewController
我是不确定那是多么聪明或可行。
I would try a label
behind the tableview
then hide one of the two based on the results, but since I'm working with a TableViewController
and not a normal ViewController
I'm not sure how smart or doable that is.
我也在使用 Parse
并将子类化为a PFQueryTableViewController
:
I'm also using Parse
and subclassing as a PFQueryTableViewController
:
@interface TableViewController : PFQueryTableViewController
我可以提供所需的任何其他详细信息,请告诉我们!
I can provide any additional details needed, just let me know!
TableViewController
故事板中的场景:
TableViewController
Scene in Storyboard:
编辑:每Midhun MP,这是我正在使用的代码
Per Midhun MP, here's the code I'm using
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if ([self.stringArray count] > 0)
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
//yourTableView.backgroundView = nil;
self.tableView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height)];
noDataLabel.text = @"No data available";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
//yourTableView.backgroundView = noDataLabel;
//yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundView = noDataLabel;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
这是我得到的视图,它仍然有分隔线。我觉得这是一个很小的变化,但我不确定为什么分隔线出现?
And here's the View I'm getting, it still has separator lines. I get the feeling that this is some small change, but I'm not sure why separator lines are showing up?
推荐答案
您可以轻松实现使用 backgroundView
属性 UITableView
。
You can easily achieve that by using backgroundView
property of UITableView
.
目标C:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if (youHaveData)
{
yourTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
yourTableView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, yourTableView.bounds.size.width, yourTableView.bounds.size.height)];
noDataLabel.text = @"No data available";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
yourTableView.backgroundView = noDataLabel;
yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
Swift:
func numberOfSections(in tableView: UITableView) -> Int
{
var numOfSections: Int = 0
if youHaveData
{
tableView.separatorStyle = .singleLine
numOfSections = 1
tableView.backgroundView = nil
}
else
{
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
noDataLabel.text = "No data available"
noDataLabel.textColor = UIColor.black
noDataLabel.textAlignment = .center
tableView.backgroundView = noDataLabel
tableView.separatorStyle = .none
}
return numOfSections
}
backgroundView
Property
backgroundView
Property
表格视图的背景视图。
Swift
var backgroundView:UIView?
Objective-C
@property(非原子,readwrite,保留)UIView * backgroundView
表视图的背景视图会自动调整大小以匹配表视图的
大小。此视图作为表
视图的子视图放置在所有单元格,标题视图和页脚视图后面。
A table view’s background view is automatically resized to match the size of the table view. This view is placed as a subview of the table view behind all cells, header views, and footer views.
您必须将此属性设置为nil才能设置
表格视图的背景颜色。
You must set this property to nil to set the background color of the table view.
这篇关于如果没有表格视图结果,则显示“无结果”在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!