SRMR SRMR - 3 months ago 23
iOS Question

If no Table View results, display "No Results" on screen

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?

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.


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
Scene in Storyboard:


enter image description here

EDIT: 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?

enter image description here

Answer

You can easily achieve that by using backgroundView property of UITableView.

Objective 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 numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    var numOfSections: Int = 0
    if youHaveData
    {
        yourTableView.separatorStyle = .SingleLine
        numOfSections                = 1
        yourTableView.backgroundView = nil
    }
    else
    {
        let noDataLabel: UILabel     = UILabel(frame: CGRectMake(0, 0, yourTableView.bounds.size.width, yourTableView.bounds.size.height))
        noDataLabel.text             = "No data available"
        noDataLabel.textColor        = UIColor.blackColor()
        noDataLabel.textAlignment    = .Center
        yourTableView.backgroundView = noDataLabel
        yourTableView.separatorStyle = .None
    }
    return numOfSections
}

Reference UITableView Class Reference

backgroundView Property

The background view of the table view.

Declaration

Swift

var backgroundView: UIView?

Objective-C

@property(nonatomic, readwrite, retain) UIView *backgroundView

Discussion

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.

You must set this property to nil to set the background color of the table view.

Comments