【发布时间】:2015-10-26 14:20:48
【问题描述】:
我有一个 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>
)
它是保存对象。搜索时,我想搜索姓氏。
所以,当我搜索姓氏时,它是空的。
【问题讨论】:
标签: ios objective-c uitableview uisearchbar nspredicate