ios - UISearchDisplayController 显示解析结果

标签 ios uitableview parse-platform uisearchdisplaycontroller

我已经在用 Parse.com 制作的 UITableView 中隐含了完全可用的 UISearchDisplayController。无论如何,当我进行搜索时,我总是只得到以该字母开头的结果。我想显示包含该字母的每个数据。

例如:我在搜索中输入了 Om ,它只搜索了我:Oman and Ombudsman 。我的 UITableView 中还有一个名为 Community 的单元格。您看到这个词也包含 om 但我的搜索没有显示任何内容。它仅显示以这些字母开头的单词。

有人可以帮助我吗?谢谢。

@implementation TableViewController3
@synthesize MainTable;
@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showDetaill"]) {
        {
            DetailViewController3 *sdvc = (DetailViewController3 *)[segue destinationViewController];


            if(self.searchDisplayController.active) {
                NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
                PFObject *object = [self.objects objectAtIndex:indexPath.row];

                object = (PFObject *)[[self searchResults]objectAtIndex:[[[[self searchDisplayController]searchResultsTableView]indexPathForSelectedRow]row]];
                sdvc.objc = object;

            }   else {

                NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
                PFObject *object = [self.objects objectAtIndex:indexPath.row];
                DetailViewController3 *detailViewController = [segue destinationViewController];
                detailViewController.objc = object;


            }

        }
    }
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

    self.tableView.tableHeaderView = self.searchBar;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;



    self.searchResults = [NSMutableArray array];

    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];


-(void)filterResults:(NSString *)searchTerm {


        PFQuery *query = [PFQuery queryWithClassName: @"Countries"];
        [query whereKey:@"CountryTitle" hasPrefix:searchTerm];
        query.limit = 50;

        [query findObjectsInBackgroundWithTarget:self selector:@selector(callbackWithResult:error:)];
    }




- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

- (void)callbackWithResult:(NSArray *)celebrities error:(NSError *)error
{
    if(!error) {
        [self.searchResults removeAllObjects];
        [self.searchResults addObjectsFromArray:celebrities];
        [self.searchDisplayController.searchResultsTableView reloadData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {


        return self.objects.count;

    } else {

        return self.searchResults.count;

    }
}




- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 70.0f;
}



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

    NSString *uniqueIdentifier = @"MainCell";
    CustomCell3 *cell = nil;
    cell = (CustomCell3 *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];




    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell3 class]])
            {
                cell = (CustomCell3 *)currentObject;
                break;
            }
        }
    }

    if (tableView == self.tableView) {
        cell.MainTitle.text = [object objectForKey:@"CountryTitle"];
        cell.DescriptTitle.text = [object objectForKey:@"DescriptTitle"];
        [cell.FlagTitle setImageWithURL:[NSURL URLWithString:[object objectForKey:@"ImaURL"]]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    }






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

        PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
        NSString *content = [searchedUser objectForKey:@"CountryTitle"];
        NSString *desco = [searchedUser objectForKey:@"DescriptTitle"];
        cell.DescriptTitle.text = desco;
        cell.MainTitle.text = content;
        NSString *image = [searchedUser objectForKey:@"ImaURL"];

    [cell.FlagTitle setImageWithURL:[NSURL URLWithString:image]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
     cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
     cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];



 }

return cell;
}





@end

最佳答案

使用 - (void)whereKey:(NSString *)key containsString:(NSString *)substring 而不是 - (void)whereKey:(NSString *)key hasPrefix:(NSString * )前缀

-(void)filterResults:(NSString *)searchTerm {


    PFQuery *query = [PFQuery queryWithClassName: @"Countries"];
     [query whereKey:@"CountryTitle" containsString:searchTerm];
    query.limit = 50;

    [query findObjectsInBackgroundWithTarget:self selector:@selector(callbackWithResult:error:)];
}

如果您使用hasPrefix:,那么搜索结果将以您的搜索字符串开头。

关于ios - UISearchDisplayController 显示解析结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21469519/

相关文章:

objective-c - 如何将 UIImage 添加到分组的 UITableViewCell,使其圆角?

ios - 从 UITableView 选择行时将核心数据对象传递给另一个 View Controller

ios - 将用户的消息传递到另一个 TableView

ios - 成功登录时解析切换 View Controller

iphone - UIAlertView 弹窗之间的 UIGestureRecognizer 事件

iphone - 触发我的应用程序和打开电子邮件附件的版本限制

iOS 自定义框架、库和 bundle

ios - 奇怪的行为 : save video recorded within app?

iphone - 滚动时 UITableView 未正确更新

javascript - 获取 Parse.com 中尚未建立关系的对象列表 Angular javascript