2016-09-29 65 views
0

我的搜索tableView是空的,但我的新數組更改爲updateSearchResultsForSearchController。我是iOS的新手,一直試圖製作一張表格來搜索我的用戶朋友。我正確地處理這個問題嗎?我搜索了整天和其他帖子,在這裏我已經看到有initWithSearchResultsController:nil但這似乎並不奏效。UISearchController不顯示錶格視圖

#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; 
} 
+0

確保您已爲表視圖傳遞了有效的數據源委託。 – Itachi

+0

我在我的.h文件中有,有沒有我失蹤的UISearchController?從我的理解UITableViewController已經有了一個有效的tableDataSource,請糾正我,如果我錯了。 @Itachi – waffles

+0

我的意思是協議UITableViewDataSource ... – Itachi

回答

0

您是否將searchcontroller或resultscontroller添加到您當前的視圖層次?

+0

我不確定我是否相信我是因爲resultsController.tableView顯示,但是一旦搜索處於活動狀態,它就會顯示空白。 – waffles