Display Alert if tableView has no Results

2019-09-28 05:31发布

问题:

I previously thought I could answer this question with a simple array count (my mistake). I am trying to display an alert to the user id a tableView has no results. I cannot simply place the count in the viewDidAppear method because the web query that populates the JSON and tableView takes a few seconds to populate. I cannot simply place this in:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath {

    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] 
    lastObject]).row){
    //loading complete
    }
}

Because nothing gets called in this method if there are no results. Does anyone have any recommendations for this - been searching for over 4 hours and have pulled nearly all my hair out.

Please feel free to un-downvote this questions if it has been made more suitable for SO - this questions massive downvotes have cost me my ability to ask questions...

回答1:

You cannot check the row count in viewDidAppear because an asynchronous NSURLConnection is used to fetch the JSON data that populates the table view.

The correct way is to call reloadData in connectionDidFinishLoading, after you have updated your data source with the response from the URL request. At that point you know if the number of rows is zero or not, and there is no need to wait for the table view update to complete.



回答2:

NSInteger count = [array count];


回答3:

NSlog("array count : %d",[array count];

This will give you array count in your console.