使用NSPredicate的搜索栏不起作用

时间:2014-07-09 14:17:23

标签: ios uisearchbar nspredicate

我通过搜索栏为编辑好友制作了视图。我的添加/删除朋友工作正常,但我在添加/删除朋友时遇到了问题:SEARCHBAR ... 我的搜索栏很好地查找了我点击的电子邮件,但订单更改了: 如果我在搜索栏中找到2封电子邮件,则2封电子邮件将是我的财产中的第2封电子邮件" Allfriend",而不是我在搜索栏中找到的电子邮件... 在NSLog之后是否有任何代码要在didSelectRowAtIndexPath中完成?

我让你看到我的代码,告诉我你是否看到了问题:

editfriend.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface EditFriendsViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>


@property (nonatomic, strong) NSArray *allUsers;
@property (nonatomic, strong) PFUser *currentUser;
@property (nonatomic, strong) NSMutableArray *friends;
@property (strong, nonatomic) NSArray *searchResults;
@property (nonatomic, strong) PFUser *user;
@property (nonatomic, strong) NSMutableArray *filteredArray;


@property (nonatomic, strong) UIImage *MindleNav;



-(BOOL)isFriend:(PFUser*)user;

@end

editfriend.m:

#import "EditFriendsViewController.h"

@interface EditFriendsViewController ()

@end

@implementation EditFriendsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:self.MindleNav];

    self.searchResults = [[NSArray alloc] init];


    PFQuery *query = [PFUser query];
    [query orderByAscending:@"email"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else {
            self.allUsers = objects;
            //            self.user = [objects objectAtIndex:0];
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    }];

    self.currentUser = [PFUser currentUser];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.searchResults count];
    }
    else
    {
        return [self.allUsers count];
    }
}

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


    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [[self.searchResults objectAtIndex:indexPath.row] email];
    } else {
        cell.textLabel.text = user.email;
    }


    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}


#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];
    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"CLICK search");
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        NSLog(@"CLICK");
    }

    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryNone;

        for(PFUser *friend in self.friends) {
            if ([friend.objectId isEqualToString:user.objectId]) {
                [self.friends removeObject:friend];
                break;
            }
        }

        [friendsRelation removeObject:user];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.friends addObject:user];
        [friendsRelation addObject:user];
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

#pragma mark - Helper methods
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"email beginswith[c] %@", searchText];
    self.searchResults = [self.allUsers filteredArrayUsingPredicate:predicate];

}

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

    return YES;
}


- (BOOL)isFriend:(PFUser *)user {
    for(PFUser *friend in self.friends) {
        if ([friend.objectId isEqualToString:user.objectId]) {
            return YES;
        }
    }

    return NO;
}

@end

1 个答案:

答案 0 :(得分:1)

您的数组(可能)包含PFUser个对象,谓词为:

[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText]

BEGINSWITH仅适用于字符串,您尝试将其应用于PFUser个对象。这是崩溃的原因。如果要搜索名称以searchText开头的用户,请将谓词更改为:

[NSPredicate predicateWithFormat:@"username beginswith[c] %@", searchText]