与NSObjects IOS的​​NSPredicate问题

时间:2015-10-26 14:20:49

标签: ios objective-c uitableview uisearchbar nspredicate

我有一个NSObject:

@interface ContactAlphaB : NSObject

+ (ContactAlphaB *)contactWithFirstName:(NSString *)firstName lastName:(NSString *)lastName username:(NSString *)username
 linkAvatar:(NSString *)linkAvatar registerType:(NSString *)registerType
email:(NSString *)email mobile:(NSString *)mobile;

@end

我已保存此对象中的所有信息。

@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;
@property (nonatomic) NSString *username;
@property (nonatomic) NSString *linkAvatar;
@property (nonatomic) NSString *registerType;
@property (nonatomic) NSString *stremail;
@property (nonatomic) NSString *strmobile;

我在tableView中显示了

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.sectionIndexTitles;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sectionIndexTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *sectionIndexTitle = self.sectionIndexTitles[section];
    return [self.alphabetizedDictionary[sectionIndexTitle] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellListsFriend *cell = [tableView dequeueReusableCellWithIdentifier:@"cellListsFriend" forIndexPath:indexPath];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    ContactAlphaB *contact = [self objectAtIndexPath:indexPath];
    cell.lblNameFriend.text = contact.fullName;    
    // Rounded Rect for cell image
    cell.imgAvatarFriends.layer.borderWidth=2;
    cell.imgAvatarFriends.layer.borderColor=[[UIColor whiteColor]CGColor];
    [cell.imgAvatarFriends.layer setCornerRadius:cell.imgAvatarFriends.frame.size.width/2];
    [cell.imgAvatarFriends.layer setMasksToBounds:YES];

    return cell;
}

- (void)setContacts:(NSArray *)contacts {
    self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:contacts usingKeyPath:@"lastName"];
    self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];

    [self.tableView reloadData];
}

- (ContactAlphaB *)objectAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
    return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}

使用此代码,我可以在tableView中显示所有我的朋友列表。

现在,我想做搜索功能(搜索我的朋友)。

所以,我正在使用NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF == %@", searchString];来处理这个功能。

但是,我无法在NSObject中使用filteredArrayUsingPredicate进行搜索。

如何进行搜索功能。

请帮助我!

*****************编辑:

NSArray *arr = [FriendList MR_findAll];
    if (arr.count > 0) {
        for (int i = 0; i < arr.count; i++) {
            FriendList *friendList = [arr objectAtIndex:i];
            [delegate.arrFriendListsAdded addObject:friendList.username];
            ContactAlphaB *contact = [ContactAlphaB contactWithFirstName:friendList.firstName
                                                                lastName:friendList.lastName
                                                                username:friendList.username                                                            
                                                              linkAvatar:friendList.avatar
                                                            registerType:[NSString stringWithFormat:@"%@", friendList.registerType]
                                                                   email:friendList.email
                                                                  mobile:friendList.phoneNumber
                                      ];

            [_mucontacts addObject:contact];

        }
    }

_mucontacts是NSMutableArray。

当我记录这个数组时:

<__NSArrayM 0x4e48050>(
<ContactAlphaB: 0x4f55b90>,
<ContactAlphaB: 0x13e8dc0>,
<ContactAlphaB: 0x4f2bd30>,
<ContactAlphaB: 0x4e6a490>
)

是保存对象。搜索时,我想搜索姓氏。

所以,当我搜索姓氏时,它会变空。

1 个答案:

答案 0 :(得分:0)

试试这个

  NSString *searchString = @"your last name here";
  NSPredicate *lastname = [NSPredicate predicateWithFormat:@"self.lastname contains[cd] %@", searchString];
  NSArray *filteredArray = [mucontacts filteredArrayUsingPredicate:lastname];