ios - NSPredicate 的搜索栏不起作用

标签 ios uisearchbar nspredicate

我已经制作了一个带有搜索栏的编辑 friend 的 View 。我的添加/删除 friend 工作正常,但我在使用搜索栏添加/删除 friend 时遇到问题... 我的搜索栏很好地找到了我正在点击的电子邮件,但顺序发生了变化: 如果我通过搜索栏找到 2 封电子邮件,则这 2 封电子邮件将是我的属性“Allfriend”中的第 2 封电子邮件,而不是我通过搜索栏找到的电子邮件... 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

最佳答案

您的数组(可能)包含 PFUser 对象并且谓词是:

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

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

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

关于ios - NSPredicate 的搜索栏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24656181/

相关文章:

ios - 如何在iOS中实现Android的getViewById?

ios - 如何在 SearchBar 中使用 textDidChange 进行搜索

ios - 使用 UISearchController 在 Navigationbar 中实现搜索栏

ios - Red Bear Lab BLE Shield 无法连接到我的 iPhone

ios - Pod 规范中的 Cocoapods 依赖项不起作用

导航栏中不出现小延迟后添加的 iOS 搜索栏

cocoa - NSPredicate:搜索 NSNumber (Cocoa)

ios - 根据 key 的值从字典的 NSArray 中获取公共(public)元素

ios - NSPredicate - 获取具有一对多关系的实体

ios - iOS Swift Web Socket SRWebSocket设置响应超时