If No Table View Results, Display "No Results" on Screen

If no Table View results, display No Results on screen

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

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.

How to display search results, and no result text if no result?

func numberOfSections(in tableView: UITableView) -> Int {

if (self.data.count == 0)
{
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: tableView.bounds.size.height))
noDataLabel.text = "No Result Found."
noDataLabel.textColor = UIColor.red
noDataLabel.textAlignment = .center
self.tableView.backgroundView = noDataLabel
self.tableView.backgroundColor = UIColor.white
self.tablevİEW.separatorStyle = .none
}
return 1

}

self.arrayDealPage is array & self.dealsTable is tableView Hope, it might help u and delegate and datasource protocol added.

Show no data in table view - when no data return from api

Try this:

func numberOfSectionsInTableView(tableView: UITableView) -> Int 
{

var numOfSection: NSInteger = 0

if YourArraydata.count > 0
{

self.tableView.backgroundView = nil
numOfSection = 1

}
else
{

var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
noDataLabel.text = "No Data Available"
noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
noDataLabel.textAlignment = NSTextAlignment.Center
self.tableView.backgroundView = noDataLabel

}

return numOfSection
}

Sample Image

Show label if Table View is empty

I ended up using the following code to check if my Core Data database is empty. Works brilliantly. This must go in the CoreDataController.m file.

NSLog(@"Total number of rows = %d ", totalNumberOfRowsInDatabase);

if (totalNumberOfRowsInDatabase == 0)
{

NSLog(@"Database is empty");
UIImage *image = [UIImage imageNamed:@"emptyTable.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:self.tableView.bounds];
[self.tableView setBackgroundView:imageView];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView setBackgroundColor:[UIColor clearColor]];

}
else
{
[self.tableView setBackgroundView:nil];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.tableView setBackgroundColor:[UIColor whiteColor]];
}

return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];

UITableView No Data Screen

You want to return one cell reporting on the lack of data.

If you're keeping your cell data in an array that's a class property (let's say NSArray *listings), you can go:

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if ([self.listings count] == 0) {
return 1; // a single cell to report no data
}
return [self.listings count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.listings count] == 0) {
UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease];
cell.textLabel.text = @"No records to display";
//whatever else to configure your one cell you're going to return
return cell;
}

// go on about your business, you have listings to display
}

UISearchDisplayController with no results tableView?

here is a little trick that i just figured out
and also you have to return 0 results while editing searchstring

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
savedSearchTerm = searchString;

[controller.searchResultsTableView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.8]];
[controller.searchResultsTableView setRowHeight:800];
[controller.searchResultsTableView setScrollEnabled:NO];
return NO;
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
// undo the changes above to prevent artefacts reported below by mclin
}

i think you'll figure out what to do next

UISearchController: show results even when search bar is empty

If your searchBar is active but has no text, the underlying tableView results are shown. That's the built-in behavior, and the reason why searchResultsController is hidden for that state.

To change the behavior when search is active but not filtering, you're going to have to show the searchResultsController when it is normally still hidden.

There may be a good way to accomplish this via <UISearchResultsUpdating> and updateSearchResultsForSearchController:. If you can solve it via the protocol, that's the preferred way to go.

If that doesn't help, you're left with hacking the built-in behavior. I wouldn't recommend or rely on it, and it's going to be fragile, but here's an answer if you choose that option:

  1. Make sure your tableViewController conforms to <UISearchControllerDelegate>, and add

    self.searchController.delegate = self;

  2. Implement willPresentSearchController:

    - (void)willPresentSearchController:(UISearchController *)searchController
    {
    dispatch_async(dispatch_get_main_queue(), ^{
    searchController.searchResultsController.view.hidden = NO;
    });
    }

    This makes the searchResultsController visible after its UISearchController set it to hidden.

  3. Implement didPresentSearchController:

    - (void)didPresentSearchController:(UISearchController *)searchController
    {
    searchController.searchResultsController.view.hidden = NO;
    }

For a better way to work around the built-in behavior, see malhal's answer.

UISearchDisplayController configure no results view not to overlap tableFooterView

First of all, it is important to keep in mind that using UISearchDisplayController is a trade-off between customizability and convenience. You get a lot of automatic stuff for free, but if you require a lot of customization it might not suit your needs. It works best as a drop-in solution, and if you'd really like to use it, I recommend you work your app around it instead of trying to hack its behavior.

With that said, why don't you make the UITableView return a single row when there are no results? This row could either be empty, display some boilerplate text or an image (sad face or something humorous that matches the app).

Having a single row to be presented, the default "No Results" label from UISearchDisplayController would not be shown. This is the default behavior for a lot of Apps, like Instagram (check out the Explore tab), Kindle and the default Notes.app, although they are probably not using UISearchDisplayController.

If you find that this workaround doesn't fit your app, you could also try making the "Missing a Brand" view into the header view of the first section of the UISearchResultsTableView (instead of the footer view). That way, the "No Results" label would be displayed below it. Combining this idea with a single empty row would even work better.

A third option would be to insert this view on top of the UISearchResultsTableView. This could be done every time there were no results (properly removing it when necessary) or just a single time, using its hidden property to show or hide it instead.

Any other method for hiding this label is probably hacky, like this one: https://stackoverflow.com/a/11715841/382834. Although easier on your current logic, it might have negative consequences later on, so it's possibly the worst path to follow.

Handling an empty UITableView. Print a friendly message

UITableView's backgroundView property is your friend.

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.

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.



Related Topics



Leave a reply



Submit