简体   繁体   English

Obj-c - 如果没有搜索结果,在 tableview 中返回 1 个单元格?

[英]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.当我的用户在 tableview 中搜索某些内容时,没有返回结果时,我的应用程序在 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视图控制器.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.但是,如果您坚持,您可以创建一个默认对象并使用该对象初始化您的 searchResults 数组,并从 numberOfRows 方法返回 1。 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:而且,您可以大大简化 cellForRowAtIndexPath 代码,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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