日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用搜索栏过滤collectionView(按照首字母)

發布時間:2023/12/1 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用搜索栏过滤collectionView(按照首字母) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.解析json數據 NSDictionary *citiesDic = [CoreJSONSerialization coreJSONSerialization:@"cities"]; NSDictionary *infor = [citiesDic objectForKey:@"infor"]; NSArray *listItems = [infor objectForKey:@"listItems"];? 2.存儲數據 ? for (NSDictionary *dic in listItems) { // 存儲數據到model中去 CityModel *model = [[CityModel alloc] init]; model.name = [dic objectForKey:@"name”];//城市名 ??????? model.charindex = [dic ·objectForKey:@"charindex”];//城市名首字母 //把城市名首字母組成數組(城市名首字母在下面要作為字典的key) NSMutableArray *charIndexArray = [_cityDicForCharIndex objectForKey:model.charindex];//_cityDicForCharIndex是以首字母為key,以?對應該key值的所有的model組成的數組 作為value 的字典 if (!charIndexArray) {//本次for循環對應的首字母(model.charindex) 它對應的value值不存在
??????????? // 如果不存在就要初始化城區數據要存儲的對應的數組
??????????? charIndexArray = [[NSMutableArray alloc] init];
???????????
??????????? [charIndexArray addObject:model];
???????????
??????????? // 還要要存儲的對應的數組寫入到字典
??????????? [_cityDicForCharIndex setValue:charIndexArray forKey:model.charindex]; }else {//如果存在,就直接往value數組中添加數據
??????????? [charIndexArray 3addObject:model];
??????? } [_citiesList addObject:model];//所有model都存放在這個數組 } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? 3.數據排序(按首字母排序) NSArray *keyArray = [_cityDicForCharIndex allKeys];//_cityDicForCharIndex字典里所有的key值 NSArray *resultArray = [keyArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSComparisonResult result = [obj1 compare:obj2 options:NSCaseInsensitiveSearch];//NSCaseInsensitiveSearch不區分大小寫 return result;//排完序的key值數組 }]; 4.數據傳遞 [_charArray addObjectsFromArray:resultArray]; //存放所有的key值(已排序) [_searchCities addObjectsFromArray:_citiesList];//搜索欄過濾出來的數組 (默認顯示所有的城市) // 數據傳遞 _citiesCollectionView.charArray = _charArray; //傳遞key值給CollectionView _citiesCollectionView.cityDic = _cityDicForCharIndex; //傳遞字典給CollectionView 5.初始化搜索欄 ? UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, kScreenW, 44)]; searchBar.delegate = self;//?代理要實現<UISearchBarDelegate>協議 searchBar.searchBarStyle = UISearchBarStyleProminent;//搜索欄的風格 searchBar.showsCancelButton = YES;//顯示取消按鈕 searchBar.placeholder = @"搜索城市";//默認的顯示文本 [self.view addSubview:searchBar]; 6.在搜索欄的代理方法中實現tableView的過濾 // (1)開始編輯搜索內容 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [searchBar becomeFirstResponder];// 顯示鍵盤(第一響應者) // 保證搜索的標示圖剛剛顯示時? 加載所有的數據為全部的城市名 // 重新進行數據傳遞 _citiesCollectionView.charArray = _charArray; //傳遞key值給CollectionView _citiesCollectionView.cityDic = _cityDicForCharIndex; //傳遞字典給CollectionView [_citiesCollectionView reloadData]; } //(2)?輸入搜索文本 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { // 刷新數據 _citiesCollectionView.charArray = @[searchText]; //設置key值數組為輸入的值 NSMutableDictionary *dic =[[NSMutableDictionary alloc]init]; dic = [_modelDicForCharIndex objectForKey:searchText]; //以searchText值為key的value值(model數組) [_citiesCollectionView.modelDic setValue:searchText forKey:[_modelDicForCharIndex objectForKey:searchText]]; //設置value值 } //(3)取消搜索 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

??? [searchBar resignFirstResponder];//隱藏鍵盤 [searchBar setText:@""];//取消搜索內容 }

// (4)結束編輯搜索內容 - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; //失去第一響應者身份 }

轉載于:https://www.cnblogs.com/zh-li/p/5125284.html

總結

以上是生活随笔為你收集整理的使用搜索栏过滤collectionView(按照首字母)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。