Definition of meaningless data:

MateCellModel * model = [[MateCellModel alloc] init]; Model. name = @" "; model.avatar = @"http://www.feizl.com/upload2007/2011_11/111114031172443.jpg"; Model. cityName = @" cityName "; Model. countyName = @" "; MateCellModel * model2 = [[MateCellModel alloc] init]; Model2. name = @" zhang Jingxue "; model2.avatar = @"http://www.ghost64.com/qqtupian/zixunImg/local/2016/11/30/14804708832644.jpg"; Model2. cityName = @" chengdu "; Model2.countyname = @" jinniu district "; MateCellModel * model3 = [[MateCellModel alloc] init]; Model3. name = @" "; model3.avatar = @"http://www.feizl.com/upload2007/2012_12/121225160256954.jpg"; Model3. cityName = @" chengdu "; Model3. countyName = @" qingbaijiang "; MateCellModel * model4 = [[MateCellModel alloc] init]; Model4. name = @" "; model4.avatar = @"http://cdn.clickme.net/Gallery/2014/08/30/1a751a31715abc7777826ecb95ae6bf6.png"; Model4. cityName = @" chengdu "; Model4. countyName = @" wuhou "; MateCellModel * model5 = [[MateCellModel alloc] init]; Model5. name = @" zhang Xiaoyu "; model5.avatar = @"http://img.duoziwang.com/2017/04/13/B2037.jpg"; Model5. cityName = @" chengdu "; Model5. countyName = @" jinjiang "; MateCellModel * model6 = [[MateCellModel alloc] init]; Model6.name = @" jiangchengzi "; model6.avatar = @""; Model6.cityname = @" chengdu "; Model6.countyname = @" 新 新 的 "; MateCellModel * model7 = [[MateCellModel alloc] init]; Model7.name = @" mondo "; model7.avatar = @"http://news.shangqiuw.com/upload/News/2016-7-12/201671215224938hiyak.jpg"; Model7.cityname = @" chengdu "; Model7.countyname = @" longquanchi "; MateCellModel * model8 = [[MateCellModel alloc] init]; Model8.name = @" liu "; model8.avatar = @"http://img5.duitang.com/uploads/item/201508/03/20150803104547_4JZjn.thumb.224_0.jpeg"; Model8.cityname = @" chengdu "; Model8.countyname = @" jinjiang "; MateCellModel * model9 = [[MateCellModel alloc] init]; Model9.name = @" hansaby "; model9.avatar = @"https://wanzao2.b0.upaiyun.com/system/pictures/25529295/original/7f7aa77ce7b350ef.png-550"; Model9.cityname = @" chengdu "; Model9.countyname = @" wuhou "; MateCellModel * model10 = [[MateCellModel alloc] init]; Model10.name = @" zidane "; model10.avatar = @"http://news.7m.com.cn/news/upload_img/20090621/4uFgOdYiWG7b.jpg"; Model10.cityname = @" chengdu "; Model10.countyname = @" jinjiang ";Copy the code

Get to the point!!

Global variables:

{ UISearchBar * _mySearchBar; // UITextField * _searchField; // Input box (search box) NSMutableArray * _saerch_DataSource; // Index (A~Z) array BOOL _isUsingSearchBar; // If search box is used}Copy the code

Properties:

/ / responsible for the original class @ the property of the data packet (nonatomic, strong) UILocalizedIndexedCollation * localizedCollation; @property (nonatomic,strong) NSMutableArray * dataArray; @property (nonatomic, copy)NSArray * handleContactObjects; @property (nonatomic,strong) NSMutableArray *searchResultArr; // Search resultsCopy the code

Gets the input field for the defined search box (_mySearchBar) : _searchField

_searchField = [_mySearchBar valueForKey:@"_searchField"];
Copy the code

Use of indexes:
/ index of * * * / self. MatesTabV. SectionIndexColor = [UIColor colorWithRed: 42. The f / 255 f green: 170. F / 255 f blue: 255. F / 255. F alpha:1.f]; / / change the color of the index: light blue self. MatesTabV. SectionIndexBackgroundColor = [UIColor clearColor]; / / changes the background color of the index of the self. MatesTabV. SectionIndexTrackingBackgroundColor = [UIColor redColor]; _saerch_DataSource = [[NSMutableArray alloc] init]; // initialize index data for(char c = 'A'; c <= 'Z'; C ++) {// Store: 'A' ~ 'Z' [_saerch_DataSource addObject:[NSString stringWithFormat:@"%c",c]]; if (c == 'Z') { [_saerch_DataSource addObject:[NSString stringWithFormat:@"#"] ]; }} / / instantiate the grouping of the native class self. LocalizedCollation = [UILocalizedIndexedCollation currentCollation];Copy the code

Proxy Settings for indexes:

/ / MARK: sectionIndex (UITableViewDataSource) - (NSArray *) sectionIndexTitlesForTableView tableView: (UITableView *) {return _saerch_DataSource; // return the index array: A~Z } -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title If (_isUsingSearchBar == NO) {// Non-search state NSInteger count = 0; / / NSLog (@ "click on the title: % @ - location: % ld", the title, (long) index); MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeText; hud.label.font = [UIFont boldSystemFontOfSize:35.f]; hud.label.text = [NSString stringWithFormat:@"%@ ",_saerch_DataSource[index]]; hud.label.textAlignment = NSTextAlignmentCenter; hud.label.adjustsFontSizeToFitWidth = YES; hud.margin = 35.f; hud.offset = CGPointMake(0, 0.f); hud.removeFromSuperViewOnHide = YES; [hud hideAnimated:YES afterDelay:1.f]; hud.userInteractionEnabled = NO; // Self.view for(NSString *character in _saerch_DataSource) {if([character isEqualToString:title]) {return count; } count ++; } } return 0; }Copy the code

Effect: Light blue font with red background when selected!

For tableView proxies:

#pragma mark - UITableViewDataSource // There is only one group (section) - (NSInteger) numberOfSectionsInTableView tableView: (UITableView *) {if (_searchField. Text. Length > 0) { // Search box has text return 1; } else { return _saerch_DataSource.count; / / self. HandleContactObjects. Count}} / / search box with words, -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if (_searchField. Text. Length > 0) {/ / search box have return self. SearchResultArr. Count; } else { return ((NSArray *)self.handleContactObjects[section]).count; }} // When the search box has text, use the array generated by the search text: self.searchResultArr -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MateListTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; if (! cell) { cell = [[MateListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } the if (_searchField. Text. Length > 0) {/ / text search box with the if (self. SearchResultArr. Count > 0) {cell. The model = self.searchResultArr[indexPath.row]; } } else { if (self.handleContactObjects.count > 0 && ((NSArray *)self.handleContactObjects[indexPath.section]).count > 0) { cell.model = ((NSArray *)self.handleContactObjects[indexPath.section])[indexPath.row]; } } cell.selectionStyle = UITableViewCellSelectionStyleNone; Return cell; } // Hide the header view when the search box is used and the final array is empty (return height: 0) - (CGFloat) tableView: (UITableView *) tableView heightForHeaderInSection: (NSInteger) section {the if (_searchfield.text.length > 0) {// Return 0. F; } if (self.handleContactObjects.count > 0 && ((NSArray *)self.handleContactObjects[section]).count == 0) { return 0.f; } return SCREEN_WIDTH*60.f/750.f; } // set the header view title - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {return _saerch_DataSource[section]; }Copy the code

Click Cell to jump to the details interface:

  • The search box has text, using the array produced by the search: self.searchResultarr
  • Search box, with no text, use a good set of array: self. HandleContactObjects

#pragma mark - UITableViewDelegate -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MateDetailInfoViewController * mateDetailInfoVC = [[MateDetailInfoViewController alloc] init]; [self.navigationController pushViewController:mateDetailInfoVC animated:YES]; If (_searchField. Text. Length > 0) {/ / search box have text mateDetailInfoVC mateCellModel = self. SearchResultArr [indexPath. Row]; } else { mateDetailInfoVC.mateCellModel = ((NSArray *)self.handleContactObjects[indexPath.section])[indexPath.row]; }}Copy the code

Use of groups:

self.dataArray = @[model,model2,model3,model4,model5,model6,model7,model8,model9,model10].mutableCopy;

self.handleContactObjects = [self getTheHandleContactObjects];//真正数据源:获取分好的数组

self.searchResultArr = @[].mutableCopy;//搜索的数组


[self.matesTabV reloadData];
Copy the code

Get the grouped array :(real data source) alphabetically placed in the corresponding “A to Z” section!

# pragma mark - localizedCollation Setter - (NSArray *) getTheHandleContactObjects {/ / initializes the array of the returned array NSMutableArray * contacts = [NSMutableArray arrayWithCapacity:0]; / * * * * according to UILocalizedIndexedCollation 27 Title in 27 * * * * / store data array for (NSInteger I = 0; i < self.localizedCollation.sectionTitles.count; i++) { [contacts addObject:[NSMutableArray arrayWithCapacity:0]]; } // Start traversing the contact object, For (MateCellModel * model in self.dataarray) {NSInteger section = [self.localizedCollation sectionForObject:model collationStringSelector:@selector(name)]; // Get the name corresponding to section [contacts[section] addObject:model]; For (NSInteger I = 0; NSInteger I = 0; i < self.localizedCollation.sectionTitles.count; I ++) {// Get the array to sort NSMutableArray * tempMutableArray = contacts[I]; // Sort by nameObject name, SortDescriptor NSSortDescriptor * sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:true]; [tempMutableArray sortUsingDescriptors:@[sortDescriptor]]; contacts[i] = tempMutableArray; } // return [NSArray arrayWithArray:contacts]; }Copy the code

End result:


Use of search:
#pragma mark - UISearchBarDelegate - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { _mySearchBar.showsCancelButton = YES; // Cancel button (display) _isUsingSearchBar = YES; / / search box using state: YES / / change the color of the indexes for: transparent self. MatesTabV. SectionIndexColor = [UIColor clearColor]; return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { _mySearchBar.showsCancelButton = NO; _isUsingSearchBar = NO; // Set the index color: Light blue self. MatesTabV. SectionIndexColor = [UIColor colorWithRed: 42. The f / 255 f green: 170. F / 255 f blue: 255 f / 255 f alpha: 1. F]; return YES; } // Input box: Text change - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {// NSLog(@" input box text searchText:%@",searchText); [self.searchResultArr removeAllObjects]; NSMutableArray *tempResults = [NSMutableArray array]; NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch; NSMutableArray *contactsSource = [NSMutableArray arrayWithArray:self.handleContactObjects]; For (NSArray * array in contactsSource) {for (MateCellModel * model in array) {[tempResults addObject:model];  } } for (int i = 0; i < tempResults.count; I ++) {if (searchText.isChinese) {// Full Chinese NSString *name_storeString = [(MateCellModel *)tempResults[I] name]; NSString *city_storeString = [(MateCellModel *)tempResults[i] cityName]; NSString *County_storeString = [(MateCellModel *)tempResults[i] countyName]; NSString * total_StoreString = [NSString stringWithFormat:@"%@%@%@",name_storeString,city_storeString,County_storeString]; NSRange total_storeRange = NSMakeRange(0, total_StoreString.length); //NSRange storeRange = NSMakeRange(0,searchText.pinyin.length); if (total_StoreString.length < total_storeRange.length) { continue; } NSRange foundRange = [total_StoreString rangeOfString:searchText options:searchOptions range:total_storeRange]; If (foundrange.length) {&& Foundrange.location == 0 [self.searchResultArr addObject:tempResults[I]]; }} else {// NSString *name_storeString = [[(MateCellModel *)tempResults[I] name] pinyin]; NSString *city_storeString = [[(MateCellModel *)tempResults[i] cityName] pinyin]; NSString *County_storeString = [[(MateCellModel *)tempResults[i] countyName] pinyin]; NSString * total_StoreString = [NSString stringWithFormat:@"%@%@%@",name_storeString,city_storeString,County_storeString]; NSRange total_storeRange = NSMakeRange(0, total_StoreString.length); // NSRange storeRange = NSMakeRange(0,searchText.pinyin.length); if (total_StoreString.length < total_storeRange.length) { continue; } NSRange foundRange = [total_StoreString rangeOfString:searchText.pinyin options:searchOptions range:total_storeRange];  If (foundrange.length) {&& Foundrange.location == 0 [self.searchResultArr addObject:tempResults[I]]; } } } [self.matesTabV reloadData]; } / / "cancel" button - (void) searchBarCancelButtonClicked (UISearchBar *) the searchBar {[searchBar setText: @ ""]. [searchBar resignFirstResponder]; searchBar.prompt = nil; [self.searchResultArr removeAllObjects]; [self.matesTabV reloadData]; }Copy the code

Judge the categories of Chinese:

#import "NSString+ chinese. h" @implementation NSString (Chinese) - (BOOL)isChinese {NSString *match = @"(^[\u4e00-\u9fa5]+$)"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match]; return [predicate evaluateWithObject:self]; } - (BOOL)includeChinese {for(int I =0; i< [self length]; i++) { int a =[self characterAtIndex:i]; if( a >0x4e00&& a <0x9fff){ return YES; } } return NO; } @endCopy the code

End result:




(2017.12.08)

goyohol’s essay