Use the search bar to filter CollectionView (by first letter)

Source: Internet
Author: User

1. Parsing JSON dataNsdictionary *citiesdic = [corejsonserialization corejsonserialization:@ "Cities"]; Nsdictionary *infor = [citiesdic objectforkey:@ "infor"]; Nsarray *listitems = [infor objectforkey:@ "ListItems"]; 2. Storing DataFor (Nsdictionary *dic in ListItems) {//store data into model to citymodel *model = [[Citymodel alloc] init]; Model.name = [dic objectforkey:@ "name"];//City namemodel.CHARINDEX = [dic objectforkey:@] Charindex"];//City first letterMake an array of city initials (the city first letter is the key to the dictionary below) nsmutablearray *charindexarray = [_citydicforcharindex objectforkey: Model.charindex];//_citydicforcharindex is the first letter of the key toan array of all the model that should be the key valueThe dictionary as value if (!charindexarray) {//the first letter corresponding to the For Loop (MODEL.CHARINDEX) Its corresponding value value does not exist
Initialize the corresponding array to be stored if the city data is not present
Charindexarray = [[Nsmutablearray alloc] init];

[Charindexarray Addobject:model];

Also want to store the corresponding array to write to the dictionary
[_citydicforcharindex Setvalue:charindexarray ForKey:model.charindex]; }else {//If present, add data directly to the value array
[Charindexarray 3addobject:model];
       }       [_citieslist AddObject: model];//all model are stored in this array}                           &NBSP ;                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp   &NBSP       3. Sorting data (alphabetically)Nsarray *keyarray = [_citydicforcharindex allkeys];//_citydicforcharindex dictionary all key values Nsarray *resultarray = [KeyArray Sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {nscomparisonresult result = [Obj1 compare:obj 2 Options:nscaseinsensitivesearch];//nscaseinsensitivesearch case-insensitive return result;//array of key values}]; 4. Data transfer[_chararray Addobjectsfromarray:resultarray]; Store all key values (sorted) [_searchcities addobjectsfromarray:_citieslist];//Search bar filtered Array (default shows all cities)//data Transfer _ Citiescollectionview.chararray = _chararray; Pass the key value to Collectionview_citiescollectionview.citydic = _citydicforcharindex; Pass a dictionary to CollectionView 5. Initialize the search bar  Uisearchbar *searchbar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0, +, KSCREENW, a)];searchbar.delegate = self;/ /Agent to implement <UISearchBarDelegate> protocol Searchbar.searchbarstyle = uisearchbarstyleprominent;// Search bar Style Searchbar.showscancelbutton = yes;//Show Cancel button Searchbar.placeholder = @ "Search city";//default display text [Self.view addsubview: Searchbar]; 6. Implement TableView filtering in the proxy method of the search bar(1) Start editing search Content-(void) searchbartextdidbeginediting: (Uisearchbar *) Searchbar {    [Searchbar becomefirstresponder];//Display keyboard (first responder)    //Guaranteed search for the map just displayed   load all data for all city names    //re-pass data _citiescollectionview.chararray = _chararray; Pass the key value to Collectionview_citiescollectionview.citydic = _citydicforcharindex; Pass dictionary to Collectionview   [_citiescollectionview reloaddata];   } //(2)   Enter search text-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText {   //Refresh Data _ Citiescollectionview.chararray = @[searchtext]; Set key value array as input value nsmutabledictionary *dic =[[nsmutabledictionary alloc]init];d ic = [_modeldicforcharindex ObjectForKey : SearchText]; Value (model array) with SearchText value key  [_citiescollectionview.modeldic setvalue:searchtext forkey:[_ Modeldicforcharindex Objectforkey:searchtext]]; Set value value    } //(3) Cancel Search-(void) searchbarcancelbuttonclicked: (Uisearchbar *) SEARCHBAR {

[Searchbar resignfirstresponder];//Hide keyboard [Searchbar settext:@ ""];//Cancel Search Contents}

(4) End Edit Search Content-(void) searchbartextdidendediting: (Uisearchbar *) Searchbar {[Searchbar resignfirstresponder];//Lose First ring The identity of the person}

Use the search bar to filter CollectionView (by first letter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.