繁体   English   中英

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

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

当我的用户在 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;
    }

我真的不建议这样做,因为如果没有搜索结果,您应该返回一个空列表。 这与用户界面指南一致。 但是,如果您坚持,您可以创建一个默认对象并使用该对象初始化您的 searchResults 数组,并从 numberOfRows 方法返回 1。 像这样的东西:

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

    }

 }

而且,您可以大大简化 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;

}

我在我的应用程序中做了类似的事情。 这很丑陋,我不建议您遵循这种方式。 我这样做只是因为我对布局有点懒惰,将占位符放置在视图层次结构中的正确位置并处理所有这些隐藏/显示情况。 我的视图控制器有一个非常复杂的视图层次结构,而表视图已经拥有我需要的一切(在显示状态或工具栏时自动调整大小)。

我建议您在搜索结果为空时隐藏表格视图并将其替换为您的占位符。

试试这个它的工作原理:

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