2015-05-29 3 views
2

Мне нужна помощь в настройке функции поиска PFQTVC. У меня большая часть этого завершена, но я зациклился на том, как переключать локальные данные (self.tabledata) для удаленных данных (self.objects). Вот мой код. Я застрял в методе filterContentForSearchText.Реализация функции поиска PFQueryTableViewController

#import "ViewController.h" 

@interface ViewController() <UISearchDisplayDelegate> 

@property (nonatomic, strong) NSArray *tableData; 
@property (nonatomic, strong) NSMutableArray *searchResult; 

@end 

@implementation ViewController 

- (id)initWithCoder:(NSCoder *)aCoder 
{ 
    self = [super initWithCoder:aCoder]; 
    if (self) { 
     // Custom the table 

     // The className to query on 
     self.parseClassName = @"profs"; 

     // The key of the PFObject to display in the label of the default cell style 
     self.textKey = @"name"; 

     // Whether the built-in pull-to-refresh is enabled 
     self.pullToRefreshEnabled = YES; 

     // Whether the built-in pagination is enabled 
     self.paginationEnabled = YES; 

     // The number of objects to show per page 
     self.objectsPerPage = 500; 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

// self.tableData = @[@"One",@"Two",@"Three",@"Twenty-one"]; 
    self.searchResult = [NSMutableArray arrayWithCapacity:[self.objects count]]; 
} 

- (PFQuery *)queryForTable 
{ 

    PFQuery *query; 


     query = [PFQuery queryWithClassName:self.parseClassName]; 







    // If no objects are loaded in memory, we look to the cache first to fill the table 
    // and then subsequently do a query against the network. 
    /* if ([self.objects count] == 0) { 
    query.cachePolicy = kPFCachePolicyCacheThenNetwork; 
    }*/ 

    return query; 
} 



- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     return [self.searchResult count]; 
    } 
    else 
    { 
     return [self.objects count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     cell.textLabel.text = [object objectForKey:@"name"]; 
    } 

    return cell; 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    [self.searchResult removeAllObjects]; 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText]; 



    self.searchResult = [NSMutableArray arrayWithArray: [self.tableData filteredArrayUsingPredicate:resultPredicate]]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 

    return YES; 
} 

@end 

ответ

0

Попробуйте это:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    [self.searchResult removeAllObjects]; 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"%K CONTAINS[c] %@", YOUR_FIELD_TO_COMPARE, searchText]; 



self.searchResult = [NSMutableArray arrayWithArray: [self.tableData filteredArrayUsingPredicate:resultPredicate]]; 
} 

YOUR_FIELD_TO_COMPARE это имя поля для значения вы сравниваете в поиске, например, "name"

Смежные вопросы