The teacher asked to write a QQ interface to teach me the code problem.
Write a QQ interface, there are three groups, each group of people, and display online is not the line;
Check it out first.
Save it here because my pictures only use one, if you want to according to the different settings of people, as long as the cell inside the change in the line;
The main is to achieve click on the above group to expand the content, and then click Retract.
Needless to say, serving:
Let's start by constructing the data:
Nsarray * Initarray [email protected][@{@ "Name": @ "Friends", @ "Group": @ "YES" @ "Child": @[ @{@ "Name": @ "Zhang San", @ "state": @ "Online",}, @{@ "Nam E ": @" Harry "@" state ": @" Leave "}] }, @{@ "Name": @ "Family" @ "Group": @ "YES" @ "Child": @[@{@ "Name": @ "sister" @ "Sta" TE ": @" Online "}, @{@" Name ": @" sister "@" state ": @ "Online"}, @{@ "Name": @ "brother", @ "state": @ "leave "}, @{@ "Name": @ "brother" @ "state": @ "Leave"}] }, @{@ "Name": @ "Stranger", @ "Group" : @ "YES" @ "Child": @[@{@ "Name": @ "Xiao Wang", @ "state": @ "Online"}, @{@ "Name": @ "Xiao Li", @ "state": @ "Leave"}, @{@ "Name": @ "Little Red", @ "state": @ "Leave"}]}];
Set an array property to accept the data;
@property (nonatomic,copy) nsmutablearray * Cellarray;
Well, let's use the data to construct the interface.
The interface is simple:
A picture is one's own avatar;
One is your own web name.
An online status
Here is a table (TableView)
@interface Viewcontroller () { UITableView * Tablev;} @end
Set a tableview;
Put it inside:
Uiimageview *iconimagev = [[[Uiimageview alloc]initwithframe:cgrectmake]; Iconimagev.image = [UIImage imagenamed:@ "Icon.png"]; [Self.view Addsubview:iconimagev]; UILabel *namelabel = [[UILabel alloc]initwithframe:cgrectmake (+, +, +)]; Namelabel.text = @ "Cedar"; Namelabel.textalignment = 1;//Sets the way the text is centered [Self.view Addsubview:namelabel]; UILabel *statelabel = [[[UILabel alloc]initwithframe:cgrectmake]; Statelabel.text = @ "Online"; Namelabel.textalignment = 1; [Self.view Addsubview:statelabel]; Tablev = [[UITableView alloc]initwithframe:cgrectmake (290, +)]; Tablev.delegate = self; Tablev.datasource = self; tablev.separatorstyle=0; Tablev.backgroundcolor =[uicolor Yellowcolor]; [Self.view Addsubview:tablev];
And then we're going to construct the table.
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell *cell; Nsdictionary * dectionary = [_cellarray objectAtIndex:indexPath.row]; if ([Dectionary objectforkey:@ "Group"]) {cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylevalue2 reuseidentifier:@ "Group"]; Cell.textLabel.text = [Dectionary objectforkey:@ "Name"]; cell.textlabel.textalignment=0; Cell.backgroundcolor = [Uicolor Graycolor]; Cell.tag = 1; } else {cell = [[UITableViewCell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@ "Child"] ; Cell.imageView.image = [UIImage imagenamed: @ "Icon.png"]; Cell.textLabel.text = [Dectionary objectforkey:@ "Name"]; Cell.detailTextLabel.text = [dectionary objectforkey:@ "state"]; } return cell; }
Here I want to say, this tag, I think a very half-day, how to record this group is open or folded it, there is no way, the last thought of the tag with tag=1 to express folding, =2 said is open;
The hardest part of the following is the way it folds open;
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{nsmutabledictionary * Dictionary = [nsmutabledictionary dictionarywithdictionary:[_cellarray ObjectAtIndex:indexPath.row]]; UITableViewCell *cell = [Tablev Cellforrowatindexpath:indexpath]; NSLog (@ "%@", Cell.textLabel.text); if ([Dictionary objectforkey:@ "Group"]) {Nsarray *childarray = [Dictionary objectforkey:@ "Child"]; Nsmutablearray *patharray = [Nsmutablearray array]; if (cell.tag==1) {cell.tag=2; for (int i =0; i<childarray.count;i++) {dictionary = [Childarray objectatindex:i]; [_cellarray insertobject:dictionary atindex:i+indexpath.row+1]; Nsindexpath *path = [Nsindexpath indexpathforrow:i+indexpath.row+1 insection:0]; [Patharray Addobject:path]; } [Tablev Insertrowsatindexpaths:patharray Withrowanimation:uitableviewroWanimationautomatic]; } else {cell.tag=1; Nsmutableindexset * deleteset= [Nsmutableindexset indexset]; For (Nsdictionary *dic in Childarray) {Nsinteger row= [_cellarray indexofobject:dic]; Nsindexpath * Path = [Nsindexpath indexpathforrow:row insection:0]; [Patharray Addobject:path]; [Deleteset Addindex:row]; } [_cellarray Removeobjectsatindexes:deleteset]; [Tablev Deleterowsatindexpaths:patharray Withrowanimation:uitableviewrowanimationbottom]; } } }
The above is the specific way, if you do not understand, then go to see my other blog http://blog.csdn.net/u010123208/article/details/38176943
Well, the source of QQ interface in
http://download.csdn.net/detail/u010123208/7774851
Welcome to download