如果没有 Table View 结果,则显示“No Results";在屏幕上 [英] If no Table View results, display "No Results" on screen

查看:23
本文介绍了如果没有 Table View 结果,则显示“No Results";在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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?).

有没有最简单的方法来做到这一点?

Is there an easiest way to do this?

我会在 tableview 后面尝试一个 label 然后根据结果隐藏两者之一,但因为我正在使用 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 并将子类化为 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 Storyboard 中的场景:

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?

推荐答案

您可以通过使用 UITableViewbackgroundView 属性轻松实现这一点.

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;
}

斯威夫特:

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
}


参考 UITableView 类参考

backgroundView 属性

表格视图的背景视图.

迅捷

var backgroundView: UIView?

目标 C

@property(nonatomic, readwrite, retain) 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.

这篇关于如果没有 Table View 结果,则显示“No Results";在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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