One, create the project
Second, create the required attributes and implementation list
First, create two properties for Viewcontroller
UITableView *tableview;//Presentation List Nsarray *titlearray;//first-level list of data to show
Initialization
Tableview=[[uitableview alloc]initwithframe:cgrectmake (0, 0, Screen_width, screen_height)]; Tableview.showsverticalscrollindicator=no; Tableview.separatorstyle=uitableviewcellseparatorstylesingleline; [Self.view Addsubview:tableview];
<span style= "White-space:pre" ></span>titlearray=[[nsarray alloc]initwithobjects:@ "Friends", @ "relatives", @ "classmate", @ " "Friend" @ "forum Friends", nil];
To giveViewcontroller adding TableView-related Protocols
@interface Viewcontroller () <uitableviewdatasource,uitableviewdelegate>{ uitableview *tableview;//Show list Nsarray *titlearray;//The first level list needs to show the data }
Implement related Protocols
#pragma mark--tableviewdelegate-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ //Returns the number of rows in the list return titlearray.count;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ return 0;} -(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section{ return 50;} -(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) section{UIView *view=[[uiview Alloc]initwithframe:cgrectmake (0, 0, Screen_width,)]; UILabel *titlelabel=[[uilabel Alloc]initwithframe:cgrectmake (5, Screen_width,)]; Titlelabel.text=[titlearray Objectatindex:section]; [View Addsubview:titlelabel]; return view;}
So far, the implementation of the first-tier classification list has been implemented
Next we implement the second tier. For example, to expand a group of friends will appear in all friends groups under the Friends List
Start by creating a dictionary to store related information
Nsarray *friendsarray;//Friends List nsarray *familyarray;//relatives list nsarray *schoolmatearray;//classmates list Nsarray * friendstravelarray;//list nsarray *xianfriendsarray;//Xian friends list nsarray *strangersarray;//strangers list Nsdictionary *datadic;//Second layer of data to be presented
Initializing data
-(void) initdatasource{Titlearray=[[nsarray alloc]initwithobjects:@ "Friends", @ "relatives", @ "classmate", @ "donkey friend" @ "Friends of Xian", @ "strangers", nil]; Nsdictionary *dic01=[[nsdictionary alloc]initwithobjectsandkeys:@ "Zhang San Feng", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic02=[[nsdictionary alloc]initwithobjectsandkeys:@ "Deng Chao", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic03=[[nsdictionary alloc]initwithobjectsandkeys:@ "WU", @ "name", @ "male", @ "sex", nil]; Friendsarray=[[nsarray alloc]initwithobjects:dic01,dic02,dic03, nil]; Nsdictionary *dic11=[[nsdictionary alloc]initwithobjectsandkeys:@ "Dad", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic12=[[nsdictionary alloc]initwithobjectsandkeys:@ "brother", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic13=[[nsdictionary alloc]initwithobjectsandkeys:@ "three elder brother", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic14=[[nsdictionary alloc]initwithobjectsandkeys:@ "uncle", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic15=[[nsdictionary alloc]initwithobjectsandkeys:@ "two uncles" @ "name" @ "male" @ "sex", nil]; Familyarray=[[nsarray alloc]initwithobjects:dic11,dic12,dic13,dic14,dic15, nil]; Nsdictionary *dic21=[[nsdictionary alloc]initwithobjectsandkeys:@ "fat", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic22=[[nsdictionary alloc]initwithobjectsandkeys:@ "Xiong", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic23=[[nsdictionary alloc]initwithobjectsandkeys:@ "Small Six Sons", @ "name", @ "male", @ "sex", nil]; Schoolmatearray=[[nsarray alloc]initwithobjects:dic21,dic22,dic23, nil]; Nsdictionary *dic31=[[nsdictionary alloc]initwithobjectsandkeys:@ "Three Guns", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic32=[[nsdictionary alloc]initwithobjectsandkeys:@ "Zhenghai", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic33=[[nsdictionary alloc]initwithobjectsandkeys:@ "Wang Chongyang", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic34=[[nsdictionary alloc]initwithobjectsandkeys:@ "Mound Machine", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic35=[[nsdictionary alloc]initwithobjectsandkeys:@ "Luna", @ "name", @ "male", @ "sex", nil]; NsdictionarY *dic36=[[nsdictionary alloc]initwithobjectsandkeys:@ "Guo Jinming", @ "name", @ "male", @ "sex", nil]; Friendstravelarray=[[nsarray alloc]initwithobjects:dic31,dic32,dic33,dic34,dic35,dic36, nil]; Nsdictionary *dic41=[[nsdictionary alloc]initwithobjectsandkeys:@ "Li Xiaofeng", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic42=[[nsdictionary alloc]initwithobjectsandkeys:@ "Wang Meng", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic43=[[nsdictionary alloc]initwithobjectsandkeys:@ "Jian Li", @ "name", @ "male", @ "sex", nil]; Xianfriendsarray=[[nsarray alloc]initwithobjects:dic41,dic42,dic43, nil]; Nsdictionary *dic51=[[nsdictionary alloc]initwithobjectsandkeys:@ "Yunfei", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic52=[[nsdictionary alloc]initwithobjectsandkeys:@ "Zhang Xiao Huan", @ "name", @ "male", @ "sex", nil]; Nsdictionary *dic53=[[nsdictionary alloc]initwithobjectsandkeys:@ "Liu Lili", @ "name", @ "male", @ "sex", nil]; Strangersarray=[[nsarray alloc]initwithobjects:dic51,dic52,dic53, nil]; Datadic=[[nsdictionary ALLOC]INITWIthobjectsandkeys:friendsarray,[titlearray Objectatindex:0],familyarray,[titlearray ObjectAtIndex:1], Schoolmatearray,[titlearray Objectatindex:2],friendstravelarray,[titlearray objectatindex:3],xianfriendsarray,[ Titlearray Objectatindex:4], Strangersarray,[titlearray Objectatindex:5], nil]; }
Implement the following proxy methods
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ NSString *str=[titlearray objectAtIndex:indexPath.section]; Nsarray *arr=[datadic Objectforkey:str]; static NSString *cellidentifier = @ "Maincell"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; Cell=nil; if (cell = = nil) { cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier: Cellidentifier]; Cell.selectionstyle = Uitableviewcellselectionstylegray; } UILabel *userlabel=[[uilabel Alloc]initwithframe:cgrectmake (5, Screen_width,)]; Userlabel.text=[[arr ObjectAtIndex:indexPath.row] valueforkey:@ "name"]; [Cell.contentview Addsubview:userlabel]; return cell; }
The current position effect is as follows:
We'll talk about this section first, and we'll have a rich list of friends in the next section (including Avatar, signature, etc.)
If there is a problem can add QQ discussion
Apple Development Group: 414319235 Welcome to Join
IOS TableView Implementation QQ Friends List (i)