////MYCONCERNLISTVIEWCONTROLLER.M//Loopdiner////Created by yl on 16/4/5.//copyright©2016 year yl. All rights reserved.//#import "MyConcernListViewController.h"#import "PeopleCell.h"#import "FansAndCnnModel.h"@interfaceMyconcernlistviewcontroller () {Nsmutablearray*_datasourcearray;//array of data sourcesNsmutablearray *_userinfosarray;//information for all usersNsmutablearray *_indexarray;//Right Index} @property (nonatomic,copy) UITableView*TableView;@end@implementationMyconcernlistviewcontroller- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view_datasourcearray=[[Nsmutablearray alloc] init]; _userinfosarray=[[Nsmutablearray alloc] init]; _indexarray=[[Nsmutablearray alloc] init]; _tableview=[[UITableView alloc] Initwithframe:cgrectzero Style:uitableviewstyleplain]; _tableview.Delegate=Self ; _tableview.datasource=Self ; _tableview.separatorstyle=Uitableviewcellseparatorstylenone; [Self.view Addsubview:_tableview]; [_tableview mas_updateconstraints:^ (Masconstraintmaker *Make ) {Make.edges.equalTo (Self.view); }]; //get all the people you care about[self getallmycnn];}#pragmaMark gets all the people who are interested-(void) getallmycnn {nsmutabledictionary*paradict =[ldunity getparadict]; Nsdictionary*datadict = @{ @"fans_user_id": myUserID}; [Paradict setobject:datadict Forkey:@"Data"]; [Self.loadingview startanimating]; [[Lpafrequest sharedrequest] postrequestwithtablename:table_userfanscnn operation:operation_getcnn parameter: Paradict Successblock:^ (Afhttprequestoperation *operation,IDresponseobject) {NSLog (@"Find all the people I care about%@", Responseobject); if([responseobject[@"Success"] Boolvalue]) { for(Nsdictionary *dictinchresponseobject[@"Data"]) { if(dict[@"user_cnn"]) {Usermodel*model = [[Usermodel alloc] initwithdictionary:dict[@"user_cnn"] Error:nil]; [_userinfosarray Addobject:model]; }} [self Divideintogroupswithsourcearray:_userinfosarray]; } Else{Self.noContentLabel.text=@"a person who has no concern for the moment"; Self.noContentLabel.hidden=NO; [Self.view BringSubviewToFront:self.noContentLabel]; }} Failedblock:^ (Afhttprequestoperation *operation, Nserror *error) {[Self.loadingview stopanimating]; }]; }- (void) Divideintogroupswithsourcearray: (Nsmutablearray *) Array {//InitializeUilocalizedindexedcollation *collation =[Uilocalizedindexedcollation currentcollation]; //get the number of collation indexes 27 = a~z and #Nsinteger Indexcount =[[Collation Sectionindextitles] count]; //Initializes an array of all indexed content based on the number of indexesNsmutablearray *dataarray =[[Nsmutablearray alloc] initwithcapacity:indexcount]; for(inti =0; i < Indexcount; i++) {Nsmutablearray*mutablearray =[[Nsmutablearray alloc] init]; [DataArray Addobject:mutablearray]; } for(Usermodel *modelinchArray) { //gets the location of the value of the Name property, such as "Lindane", the first letter is L, row 11th in A~z (the first digit is 0), and the Sectionnumber isNsinteger index =[Collation Sectionforobject:model collationstringselector: @selector (name)]; //add name to the corresponding arrayNsmutablearray *mutablearray =Dataarray[index]; [Mutablearray Addobject:model]; } //sort the array in each section by the Name property for(inti =0; i < Indexcount; i++) { //iterate through the elements in the array, and each element is also an arrayNsmutablearray *personarray =Dataarray[i]; //sort elements in an array by nicknameNsarray *newpersonarray =[Collation Sortedarrayfromarray:personarray collationstringselector: @selector (name)]; Dataarray[i]=Newpersonarray; if(personarray.count) {//if the array is not empty, add the title to the index array[_indexarray addobject:[collation sectionindextitles][i]; }} _datasourcearray=[DataArray mutablecopy]; [_tableview reloaddata];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {Peoplecell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"]; if(Cell = =Nil) {Cell= [[Peoplecell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:@"Cell"]; } Usermodel*model =_datasourcearray[indexpath.section][indexpath.row]; [Cell.headimg sd_setimagewithurl:[nsurl URLWithString:model.portrait] placeholderimage:[uiimage imagenamed:@"pic"]]; Cell.nameLabel.text=Model.name; returncell;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return[_datasourcearray[section] count];}-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return_datasourcearray.count;}-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section {if([_datasourcearray[section] count]) {return -; } Else { return 0; }}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {return -;}-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section {if([_datasourcearray[section] count]) {return[[Uilocalizedindexedcollation currentcollation] sectionindextitles][section]; } Else { return @""; }}- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {[TableView deselectrowatindexpath:indexpath animated:yes];}//turn on the right index bar-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{return_indexarray;}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}/*#pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before n avigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [s] Egue Destinationviewcontroller]. Pass the selected object to the new view controller.}*/@end
Write a similar address book with Uilocalizedindexedcollation