因为公司的需求,要完成一个全新的通讯录列表。参考了网上一些demo,自己修改也先写了一个demo。
效果图:
WX20180323-221315.png
1.png
2.png
3.png
4.png
插入以及删除数据代码
#pragma mark - 展开(收起)子节点
- (void)deleteData:(NSMutableArray *)array {
for (int i = 0; i<array.count; i++) {
contact *model = [array objectAtIndex:i];
if (model.parents) {
[self deleteData:model.parents];
}
[searchResults removeObject:model];
}
}
- (void)insertData:(NSMutableArray *)array {
for (int i = 0; i<array.count; i++) {
contact *model = [array objectAtIndex:i];
self.rowCount++;
[searchResults insertObject:model atIndex:self.rowCount];
if (model.parents && model.open) {
[self insertData:model.parents];
}
}
}
- (void)deleteData:(NSMutableArray *)array didSelect:(NSInteger)section{
NSMutableArray * delete = [NSMutableArray new];
[delete addObjectsFromArray:self.countArray[section]];
for (int i = 0; i<array.count; i++) {
contact *model = [array objectAtIndex:i];
if (model.parents) {
[self deleteData:model.parents didSelect:section];
}
[delete removeObject:model];
}
[self.countArray replaceObjectAtIndex:section withObject:delete];
}
- (void)insertData:(NSMutableArray *)array didSelect:(NSInteger)section{
NSMutableArray * insert = [NSMutableArray new];
[insert addObjectsFromArray:self.countArray[section]];
for (int i = 0; i<array.count; i++) {
contact *model = [array objectAtIndex:i];
self.rowCount++;
[insert insertObject:model atIndex:self.rowCount];
if (model.parents && model.open) {
[self insertData:model.parents didSelect:section];
}
}
[self.countArray replaceObjectAtIndex:section withObject:insert];
}
实现模糊搜索
// 联系人搜索,可实现汉字搜索,汉语拼音搜索和拼音首字母搜索,
// 输入联系人名称,进行搜索, 返回搜索结果searchResults
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
searchResults = [[NSMutableArray alloc]init];
if (contactsSearchBar.text.length>0&&![ChineseInclude isIncludeChineseInString:contactsSearchBar.text]) {
for (NSArray *section in self.countArray) {
for (contact *contact in section)
{
if ([ChineseInclude isIncludeChineseInString:contact.stuName]) {
NSString *tempPinYinStr = [PinYinForObjc chineseConvertToPinYin:contact.stuName];
NSRange titleResult=[tempPinYinStr rangeOfString:contactsSearchBar.text options:NSCaseInsensitiveSearch];
if (titleResult.length>0) {
[searchResults addObject:contact];
}
else {
NSString *tempPinYinHeadStr = [PinYinForObjc chineseConvertToPinYinHead:contact.stuName];
NSRange titleHeadResult=[tempPinYinHeadStr rangeOfString:contactsSearchBar.text options:NSCaseInsensitiveSearch];
if (titleHeadResult.length>0) {
[searchResults addObject:contact];
}
}
NSString *tempPinYinHeadStr = [PinYinForObjc chineseConvertToPinYinHead:contact.stuName];
NSRange titleHeadResult=[tempPinYinHeadStr rangeOfString:contactsSearchBar.text options:NSCaseInsensitiveSearch];
}
else {
NSRange titleResult=[contact.stuName rangeOfString:contactsSearchBar.text options:NSCaseInsensitiveSearch];
if (titleResult.length>0) {
[searchResults addObject:contact];
}
}
}
}
} else if (contactsSearchBar.text.length>0&&[ChineseInclude isIncludeChineseInString:contactsSearchBar.text]) {
for (NSArray *section in self.countArray) {
for (contact *contact in section)
{
NSString *tempStr = contact.stuName;
NSRange titleResult=[tempStr rangeOfString:contactsSearchBar.text options:NSCaseInsensitiveSearch];
if (titleResult.length>0) {
[searchResults addObject:contact];
}
}
}
}
}
第一次写这类的文章,如果哪里有问题,欢迎大家提出来。
网友评论