开发者

UITableView No Data Screen

开发者 https://www.devze.com 2023-01-08 13:31 出处:网络
My UITableView get data off the internet. Sometimes it doesn\'t receive any (cell) data. It just shows a blank table.

My UITableView get data off the internet. Sometimes it doesn't receive any (cell) data. It just shows a blank table.

How can I show a message/cell to the user that no data records hav开发者_JAVA百科e been found?


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
}


You can add a test in your -tableView:cellForRowAtIndexPath: implementation and display a special UITableViewCell saying "no results available" when you get no data.

Another approach, which generally looks better, is to add a custom view to the UITableView directly. Don't forget to remove it when there are results to display.


You can add a UILabel to your view This can be created programmatically or in your xib. If your xib contains a UITableView as [self view], as is typical for a UITableViewController, then create a property for the label on your viewController

@property (nonatomic, retain) IBOutlet UILabel       *noResultsLabel;

Drag a UILabel from the library into the document window as a sibling to your "Table View". Set text properties (36pt bold font, light gray looks pretty good)
Control drag from "File's Owner" to wire the outlet to the label.

UITableView No Data Screen

In your server callback, you can add the label to the view. Here's an example:

#pragma mark -
#pragma mark Server callbacks
- (void)serviceExecutionComplete:(NSMutableArray *)results {
    [self setServerData:results];
    if ([results count] == 0)
    {
        // no results,
        CGRect viewFrame = [[self view] frame];
        [[self noResultsLabel] setFrame:CGRectMake(0, (viewFrame.size.height - 100) / 2, viewFrame.size.width, 50.0)];
        [[self view] addSubview:[self noResultsLabel]];
    }
    else
    {
            [[self noResultsLabel] removeFromSuperview];
        [self.tableView reloadData];
    }
}

You could do the same thing in tableView:numberOfRowsInSection: if that fits your design better


I was contemplating showing an emptyDataView in either header/footer, or hacking one of the row like the suggested answer from Dan.

But I think the cleanest is to create a view outside the tableView.

@property (weak, nonatomic) IBOutlet UIView *emptyDataView;

Then hide/show the view in numberOfRowsInSection: like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];

    // Show empty data view if no data
    _emptyDataView.hidden = ([sectionInfo numberOfObjects] == 0) ? NO : YES;

    return [sectionInfo numberOfObjects];
}

This works well with Core Data NSFetchedResultsController and hide the view once there is data.


As mentioned in the below link it is easy if we create a label and set that as the background view for UITableView like below. May be it is useful for someone. http://www.appcoda.com/pull-to-refresh-uitableview-empty/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if (data) {
        self.tableView.backgroundView = nil;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        return 1;

    } else {

        // Display a message when the table is empty
        UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

        messageLabel.text = @"No data is currently available. Please pull down to refresh.";
        messageLabel.textColor = [UIColor blackColor];
        messageLabel.numberOfLines = 0;
        messageLabel.textAlignment = NSTextAlignmentCenter;
        messageLabel.font = [UIFont fontWithName:@"Palatino-Italic" size:20];
        [messageLabel sizeToFit];

        self.tableView.backgroundView = messageLabel;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }

    return 0;
}


Here i use code like:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger numOfSections = 0;
if ([jsonArray count] != 0){
    tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    numOfSections                 = 1;
    tableview.backgroundView   = nil;
}
else{
    UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableview.bounds.size.width, tableview.bounds.size.height)];
    noDataLabel.text             = @"No item found.";
    noDataLabel.textColor        = [UIColor blackColor];
    noDataLabel.textAlignment    = NSTextAlignmentCenter;
    tableview.backgroundView     = noDataLabel;
    tableview.separatorStyle     = UITableViewCellSeparatorStyleNone;
}
  return numOfSections;
}

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section{
    return [jsonArray count ];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消