首页 > 解决方案 > Obj-c - 如果没有搜索结果,返回表格视图中的 1 个单元格?

问题描述

当我的用户在 tableview 中搜索某些内容时,当没有返回任何结果时,我的应用程序会在 tableview 中显示标准的“无结果”占位符。也就是说,当不存在结果时,我想返回一个填充的单元格(填充有默认数据的单元格)。我怎样才能做到这一点?我尝试了以下方法,但仍然返回“无结果”?

视图控制器.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;
    }

标签: iosobjective-cuitableview

解决方案


试试这个它的工作:

- (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;
}

推荐阅读