UISearchController不显示tableView

时间:2016-09-29 00:42:50

标签: ios objective-c uitableview uisearchcontroller

我的搜索表格视图是空白的,但我的新数组正在updateSearchResultsForSearchController上更改。我对iOS很新,并且一直试图通过我的用户朋友搜索表。我正确地谈到这个吗?我已经在这里搜索了一整天和其他帖子,我见过initWithSearchResultsController:nil,但这似乎也不起作用。

#import "PFSearchTableViewController.h"
#import "PFSearchViewCell.h"

@interface PFSearchTableViewController (){
@property (strong, nonatomic) UISearchController * searchController;
@property (strong, nonatomic) UITableViewController * resultsController;
}
@end

@implementation PFSearchTableViewController

- (void)viewDidLoad {
   [super viewDidLoad];

   self.resultsController = [UITableViewController alloc];
   self.resultsController.tableView.dataSource = self;
   self.resultsController.tableView.delegate = self;
   self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsController];
   self.tableView.tableHeaderView = self.searchController.searchBar;
   self.searchController.searchResultsUpdater = self;

}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

   NSString * dictionaryKey = @"firstName";
   NSPredicate * predicate = [NSPredicate predicateWithFormat:@"user.%K CONTAINS[cd] %@", dictionaryKey, searchController.searchBar.text];
   self.filetedFriendsList =  [self.friendsList filteredArrayUsingPredicate:predicate];
   if (self.filetedFriendsList.count > 0) {


    NSLog(@"^^^ %@", self.filetedFriendsList);}
   [self.resultsController.tableView reloadData];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   if (tableView == self.tableView) {
      return 1;
   }else{
      return 1;

}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
    return self.friendsList.count;
}else{
    return self.filetedFriendsList.count;
}
}


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

   static NSString * cellId = @"cell";

   PFSearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

   if (cell == nil) {
      cell = [[PFSearchViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

}
   if (tableView == self.tableView) {
      NSLog(@"^^^2");
      cell.nameLabel.text = nil;
      cell.profileImageView.file =nil;
      cell.pfObject =nil;

      CALayer *imageLayer = cell.profileImageView.layer;
      [imageLayer setCornerRadius:5];
      [imageLayer setBorderWidth:0];
      [imageLayer setMasksToBounds:YES];
      [cell.profileImageView.layer setCornerRadius:cell.profileImageView.frame.size.width/2];
      [cell.profileImageView.layer setMasksToBounds:YES];
      PFFriendsObject * pfObject = [self.friendsList objectAtIndex:indexPath.row];
      cell.pfObject = pfObject;
      PFUser * user = pfObject.user;
      NSString * fullName = [NSString stringWithFormat:@"%@ %@", user[@"firstName"], user[@"lastName"]];
      cell.nameLabel.text = fullName;
      PFFile * pic = pfObject.pictureFile;
      cell.profileImageView.file = pic;
      [cell.profileImageView loadInBackground];

}else{


      cell.nameLabel.text = nil;
      cell.profileImageView.file =nil;
      cell.pfObject =nil;
      if (self.filetedFriendsList.count == 0) {
        NSLog(@"^^^3");
      }else{
        NSLog(@"^^^4");
      CALayer *imageLayer = cell.profileImageView.layer;
      [imageLayer setCornerRadius:5];
      [imageLayer setBorderWidth:0];
      [imageLayer setMasksToBounds:YES];
      [cell.profileImageView.layer setCornerRadius:cell.profileImageView.frame.size.width/2];
      [cell.profileImageView.layer setMasksToBounds:YES];
      PFFriendsObject * pfObject = [self.filetedFriendsList objectAtIndex:indexPath.row];
      cell.pfObject = pfObject;
      PFUser * user = pfObject.user;
      NSString * fullName = [NSString stringWithFormat:@"%@ %@", user[@"firstName"], user[@"lastName"]];
      cell.nameLabel.text = fullName;
      PFFile * pic = pfObject.pictureFile;
      cell.profileImageView.file = pic;
      [cell.profileImageView loadInBackground];

    }

}

return cell;
}

1 个答案:

答案 0 :(得分:0)

您是否已将searchcontroller或resultscontroller添加到当前视图层次结构中?