对象-如果没有搜索结果,是否在tableview中返回1个单元格?

时间:2019-05-10 16:09:14

标签: ios objective-c uitableview

当我的用户在表格视图中搜索某些内容时,如果没有返回任何结果,则我的应用程序会在表格视图中显示标准的“无结果”占位符。就是说,当没有结果存在时,我想返回一个填充的单元格(使用默认数据填充的单元格)。我该怎么做?我尝试了以下操作,但仍返回“无结果”?

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 objectAtIndex:0];
    }



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


       NSDictionary *userName = [searchResults objectAtIndex:indexPath.row];
        NSString *first = [userName objectForKey:@"first name"];
        NSString *last = [userName objectForKey:@"last name"];

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

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


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

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

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

        NSString *profilePath = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"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 objectForKey:@""]];
            [[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;

    }

3 个答案:

答案 0 :(得分:0)

我不建议您这样做,因为如果没有搜索结果,您应该返回一个空列表。这与用户界面准则一致。但是,如果您坚持要求,则可以创建一个默认对象,并使用该对象初始化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;

}

答案 1 :(得分:0)

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

我建议您在搜索结果为空时隐藏表格视图,并用占位符代替。

答案 2 :(得分:0)

尝试一下,它会起作用:

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