Search code examples
iosobjective-cuitableviewhpple

UITableView pops up no data before it has the data


I am using hpple to get data from web page and display it in table view, but i want to cells to display " no data" if there either is no data or it can't get the data. But because of this it flases the no data on the view controller for about a second when the data is loaded here is the code i am using:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            if ((_deli.count > 0)) {
                return _deli.count;
            }
            else {
                return 1;
            }
            break;
        case 1:
            if ((_entrees.count > 0)) {
                return _entrees.count;
            }
            else {
                return 1;
            }
            break;
     }
     return 0
}

and then

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    //[UIColor colorWithRed:191.0f/255.0f green:48/255.0f blue:48/255.0f alpha:1.0]
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:17.0f];

    if (indexPath.section == 0) {
        if ([tableView numberOfRowsInSection:0] == _deli.count ) {
            Items *thisDeli = [_deli objectAtIndex:indexPath.row];
            cell.textLabel.text = thisDeli.title;

        }
        else { cell.textLabel.text = @"No Data Avaiable";
        }

    }
    else if (indexPath.section == 1) {
        if ([tableView numberOfRowsInSection:1] == _entrees.count) {
            Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
            cell.textLabel.text = thisEntree.title;


        }
        else { cell.textLabel.text = @"No Data Avaiable";
        }

    }
}

Is there a way to delay the appearance of the "no data" phrase or only display it if it is blank or can't get it.

Heres my NSURLConnection

- (void) loadDataUsingNSURLConnection {
    NSString *url = @"http://ueat.site88.net/westTest.xml";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"response == %@", response);
}

Thanks for your help in advance.

Cheers


Solution

  • Consider making the "No Data Available" string dynamic based on whether your call has completed to load the data or not. If the call has not completed have it use "Loading" or something like that. If the call has completed have it show the existing "No Data Available" string.

    EDIT WITH EXAMPLE:

    The easiest thing to do, with the code you have, is to set your _deli and _entrees variables to nil until you've loaded them, and check for that condition when displaying your message:

    @interface YourViewController ()
    {
        NSArray *_deli;
        NSArray *_entrees;
    }
    @end
    

    Set the arrays to nil in your viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        _deli = nil;
        _entrees = nil;
    }
    

    Change the logic in your previous code to this:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        switch (section) {
            case 0:
                if (_deli == nil || _deli.count == 0) {
                    return 1;
                }
                else {
                    return _deli.count;
                }
                break;
            case 1:
                if (_entrees == nil || _entrees.count == 0) {
                    return 1;
                }
                else {
                    return _entrees.count;
                }
                break;
         }
    
         return 0;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        }
        //[UIColor colorWithRed:191.0f/255.0f green:48/255.0f blue:48/255.0f alpha:1.0]
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:17.0f];
    
        if (indexPath.section == 0) {
            if (_deli == nil)
                cell.textLabel.text = @"Loading";
            else if (_deli.count > 0) {
                Items *thisDeli = [_deli objectAtIndex:indexPath.row];
                cell.textLabel.text = thisDeli.title;
            }
            else 
                cell.textLabel.text = @"No Data Avaiable";
        }
        else if (indexPath.section == 1) {
            if (_entrees == nil)
                cell.textLabel.text = @"Loading";
            else if (_entrees.count > 0) {
                Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
                cell.textLabel.text = thisEntree.title;
            }
            else
                cell.textLabel.text = @"No Data Avaiable";
        }
    }
    

    Since your loadDataUsingNSURLConnection just writes out the response I'm not sure how you're loading the array, but you should get the idea at this point. All you need to do is just load the response into the array and call the reloadData method on your table to get the new data to appear.