简体   繁体   中英

Obj-c - If no search results, return 1 cell in tableview?

When my user searches for something inside a tableview, when no results are returned, my app shows the standard "No Results" placeholder inside the tableview. That said, when no results exist, I want to return one populated cell (cell populated with default data). How can I accomplish this? I tried the below, but I still get 'No Results' returned?

ViewController.m

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     if (tableView == self.searchDisplayController.searchResultsTableView) {

         if ([searchResults count] == 0) {
             return 1; 
         } else {
            return [searchResults count];
         }
   }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     static NSString *NetworkTableIdentifier = @"sidebarCell";
     self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;

     sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
     if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
        cell = nib[0];
     }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSDictionary *userName = searchResults[indexPath.row];
        NSString *first = userName[@"first name"];
        NSString *last = userName[@"last name"];

        [[cell username] setText:[NSString stringWithFormat:@"%@ %@", first, last]];

        NSDictionary *userlast = searchResults[indexPath.row];
        [[cell lastName] setText:userlast[@"last name"]];

        NSDictionary *userBio = searchResults[indexPath.row];
        [[cell userDescription] setText:userBio[@"userbio"]];

        NSString *area = userName[@"neighbourhood"];
        NSString *city = userName[@"city"];

        [[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", area, city]];

        NSString *profilePath = searchResults[indexPath.row][@"photo_path"];

        [cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];

        if ([searchResults count] == 0) {
            NSLog(@"SEARCH RESULTS ARE %@", searchResults);

            [[cell username] setText:[NSString stringWithFormat:@"%@", self.searchBar.text]];
            [[cell lastName] setText:userlast[@""]];
            [[cell userDescription] setText:@"This friend is not on the app (yet!) Tap to invite them."];
            [[cell areaLabel] setText:[NSString stringWithFormat:@""]];

            NSString *profileDefault = @"http://url.com/user.png";

            [cell.usermini sd_setImageWithURL:[NSURL URLWithString:profileDefault]];

            return cell;
        }
        return cell;
    }

I don't really recommend doing this, as you should return an empty list, if there are no search results. That is consistent with User Interface Guidelines. But, if you insist, you could create a default object and initialize your searchResults array with that object and return 1 from the numberOfRows method. Something like this:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  if (tableView == self.searchDisplayController.searchResultsTableView) {

    if ([searchResults count] == 0) {

      NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@“Enter First Name”, @“Enter Last Name”, @“Enter User Bio”, @“Enter Neighborhood”, @“Enter City”, @“Enter Photo Path”]
      forKeys: @[@“first_name”, @“last_name, @“userbio”, @“neighbourhood”, @“city”, @“photo_path”];

      searchResults = [NSArray arrayWithObjects: dict, nil];

      return 1; 

    } 

    else {
            return [searchResults count];

    }

 }

And, you can greatly simplify your cellForRowAtIndexPath code as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



   static NSString *NetworkTableIdentifier = @"sidebarCell";

    self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
    if (cell == nil)

    {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }



    if (tableView == self.searchDisplayController.searchResultsTableView) {

        //Note, I like to name my local variables the same as the dictionary keys just to eliminate any confusion
        NSDictionary *userObject = [searchResults objectAtIndex:indexPath.row];
        NSString *first_name = [userObject objectForKey:@"first name"];
        NSString *last_name = [userObject objectForKey:@"last name"];
        NSString *userbio = [userObject objectForKey:@“userbio”];
        NSString *neighbourhood = [userObject objectForKey:@“neighbourhood”];
        NSString *city = [userObject objectForKey:@“city”];
        NSString *photo_path = [userObject objectForKey:@“photo_path”];

        [[cell username] setText:[NSString stringWithFormat:@"%@ %@", first_name, last_name]];

        [[cell lastName] setText:last_name];

        [[cell userDescription] setText:userbio];

        [[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", neighbourhood, city]];

        [[cell usermini] sd_setImageWithURL:[NSURL URLWithString:photo_path]];

    }

    return cell;

}

I did something like this in my app. It's ugly and I'm not recommending you following this way. I did it only because I was kind of lazy about layouts and placing the placeholder in the correct place in the view hierarchy and handle all those hide/show situations. My view controller has a very complex hierarchy of views and the table view was one that has already had all I needed (resize automatically when the status or toolbar is showing).

What I suggest you is to hide the table view when there is an empty search result and substitute it with your placeholder.

Try this it's working:

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

    return numOfSections;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM