fazlurr
10/14/2014 - 10:19 AM

iOS - Show "No Matches" label when no data available in UITableView, From -> http://stackoverflow.com/a/4840621

iOS - Show "No Matches" label when no data available in UITableView, From -> http://stackoverflow.com/a/4840621

- (void)viewDidLoad
{
        nomatchesView = [[UIView alloc] initWithFrame:self.view.frame];
        nomatchesView.backgroundColor = [UIColor clearColor];

        UILabel *matchesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,320)];
        matchesLabel.font = [UIFont boldSystemFontOfSize:18];
        matchesLabel.minimumFontSize = 12.0f;
        matchesLabel.numberOfLines = 1;
        matchesLabel.lineBreakMode = UILineBreakModeWordWrap;
        matchesLabel.shadowColor = [UIColor lightTextColor];
        matchesLabel.textColor = [UIColor darkGrayColor];
        matchesLabel.shadowOffset = CGSizeMake(0, 1);
        matchesLabel.backgroundColor = [UIColor clearColor];
        matchesLabel.textAlignment =  UITextAlignmentCenter;

        //Here is the text for when there are no results
        matchesLabel.text = @"No Matches";


        nomatchesView.hidden = YES;
        [nomatchesView addSubview:matchesLabel];
        [self.tableView insertSubview:nomatchesView belowSubview:self.tableView];
    }



    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //If there is no table data, unhide the "No matches" view
        if([data count] == 0 ){
        nomatchesView.hidden = NO;
        } else {
        nomatchesView.hidden = YES;
        }

        return [data count];
    }