核心代码预览:
-(void)startSearch:(NSString *)string{
if (self.seachUserDTOList.count>0) {
[self.seachUserDTOList removeAllObjects];
}
NSString *key = string.lowercaseString;
NSMutableArray *tempArr = [NSMutableArray array];
if (![key isEqualToString:@""] && ![key isEqual:[NSNull null]] && key != nil) {
[self.storeUserDTOList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UserDTO *dto = self.storeUserDTOList[idx];
NSString *name = dto.name.lowercaseString;
NSString *namePinyin = dto.namePinYin.lowercaseString;
NSString *nameFireLetter = dto.nameFirstLetter.lowercaseString;
NSRange rang1 = [name rangeOfString:key];
if (rang1.length>0) {
[tempArr addObject:dto];
}else{
if ([nameFireLetter containsString:key]) {
[tempArr addObject:dto];
}else{
if ([nameFireLetter containsString:[key substringToIndex:1]]) {
if ([namePinyin containsString:key] ) {
[tempArr addObject:dto];
}
}
}
}
}];
[tempArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![self.seachUserDTOList containsObject:tempArr[idx]]) {
[self.seachUserDTOList addObject:tempArr[idx]];
}
}];
self.isSearchState = YES;
}else{
self.isSearchState = NO;
}
[self.tableView reloadData];
}