iOS development UI chapter-using UITableView to complete a simple QQ friends List (i)
I. Project structure and plist documents
Second, the implementation of the Code
1. Description:
Host controller directly inherits Uitableviewcontroller
yyviewcontroller.h// 02-qq Friends List (load of basic data)//// Created by Apple on 14-5-31.// Copyright (c) 2014 itcase. All rights reserved.//#import <UIKit/UIKit.h> @interface yyviewcontroller:uitableviewcontroller@end
is associated in the storyboard.
2. Code
Data Model section:
YYQQGroupModel.h file
1//2// YYQQGroupModel.h 3// 02-QQ Friends List (load of basic data) 4//5// Created by Apple on 14-5-31.6// Copyright ( c) 2014 Itcase. All rights reserved. 7//8 9 #import <foundation/foundation.h>10 @interface yyqqgroupmodel:nsobject12/**13 * name attribute 14< C8/>*/15 @property (nonatomic,copy) nsstring *name;16/**17 * online */19 @property (nonatomic,copy) NSString *online;20/**21 * friends List */23 @property (nonatomic,strong) Nsarray *friends;24 25-( Instancetype) Initwithdict: (Nsdictionary *) dict;26 + (Instancetype) qqgroupmodelwithdict: (NSDictionary *) dict;27 @end
YYQQGROUPMODEL.M file
1//2//YYQQGROUPMODEL.M 3//02-QQ friends List (load of basic data) 4//5//Created by Apple on 1 4-5-31.6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYQQGroupModel.h" #import "YYFriendsModel.h" @implementation YYQQGroupModel13-(instancetype) INI Twithdict: (nsdictionary *) dict14 {if (self=[super init]) {16//Convert dictionary to model [self setvaluesforkeyswit hdictionary:dict];18 19//define an array to save the converted model Nsmutablearray *models=[nsmutablearray Arraywithcapaci Ty:self.friends.count];21 for (nsdictionary *dict in self.friends) {Yyfriendsmodel *friends=[yyfrie Ndsmodel friendswithdict:dict];23 [Models addobject:friends];24}25 _friends=[models copy];26 }27 return self;28}29 + (instancetype) qqgroupmodelwithdict: (Nsdictionary *) dict31 {return [[Self alloc] initwithdict:dict];33}34 @end
YYFriendsModel.h file
1//2// YYFriendsModel.h 3// 02-QQ Friends List (load of basic data) 4//5// Created by Apple on 14-5-31.6// Copyright ( c) 2014 Itcase. All rights reserved. 7//8 9 #import <foundation/foundation.h>10 @interface yyfriendsmodel:nsobject12/**13 * Each friend's name is */15 @property (nonatomic,copy) nsstring *name;16/**17 * Each friend's avatar */19 @property (nonatomic , copy) NSString *icon;20/**21 * each friend's personality signature is */23 @property (nonatomic,copy) nsstring *intro;24/**25 * Whether the friend is vip26 */27 @property (nonatomic,assign,getter = isvip) BOOL vip;28-(instancetype) Initwithdict: (nsdictionary *) dict;30 + (Instancetype) friendswithdict: (nsdictionary *) dict;31 @end
YYFRIENDSMODEL.M file
1//2// YYFRIENDSMODEL.M 3// 02-QQ Friends List (load of basic data) 4//5// Created by Apple on 14-5-31.6// Copyright ( c) 2014 Itcase. All rights reserved. 7//8 9 #import "YYFriendsModel.h" @implementation YYFriendsModel12-(Instancetype) Initwithdict: ( Nsdictionary *) dict13 { if (self=[super init]) {[Self setvaluesforkeyswithdictionary:dict];16 }17 return self;18}19 + (instancetype) friendswithdict: (Nsdictionary *) dict21 { return [[Self alloc] initwithdict:dict];23}24 @end
View section
YYfriendCell.h file
1//2// YYfriendCell.h 3// 02-QQ Friends List (load of basic data) 4//5// Created by Apple on 14-5-31.6// Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import <uikit/uikit.h>10 @class yyfriendsmodel;11 @interface yyfriendcell:uitableviewcell12 @prop Erty (Nonatomic,strong) Yyfriendsmodel *friends;14 + (instancetype) Cellwithtableview: (UITableView *) tableView;16 @ End
YYFRIENDCELL.M file
1//2//YYFRIENDCELL.M 3//02-QQ friends List (load of basic data) 4//5//Created by Apple on 14-5-31. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYfriendCell.h" #import "YYFriendsModel.h" 11//Private extension @interface Yyfriendcell () @end16 @imple Mentation YYfriendCell17 + (Yyfriendcell *) Cellwithtableview: (UITableView *) tableView19 {$ static nsstring *[email& Nbsp;protected] "QQ"; Yyfriendcell *cell=[tableview dequeuereusablecellwithidentifier:identifier];22 if (cell==ni L) {23//Use the system's own style here Cell=[[yyfriendcell Alloc]initwithstyle:uitableviewcellstylesubtitle Reuseidentifie R:identifier];25 NSLog (@ "Create a Cell"),}27 return cell;28}29 (void) Setfriends: (Yyfriendsmodel *) Frien ds31 {_friends=friends;33//1. Set Avatar Self.imageview.image=[uiimage imagenamed:_friends.icon];35//2. Set nickname self.textlabel.text=_friends.name;37//3. Setup Introduction self.detailtextlabel.text=_friends.intro;39//Determine if it isClerk 40/**41 * Here's a note, what happens if you don't write the else setting to black? */43 if (_FRIENDS.ISVIP) {(Self.textlabel settextcolor:[uicolor redcolor]];45}else46 {47 [Self.textlabel settextcolor:[uicolor blackcolor]];48}49}50 @end
Master Controller Section
YYVIEWCONTROLLER.M file
1//2//YYVIEWCONTROLLER.M 3//02-QQ friends List (load of basic data) 4//5//Created by Apple on 14-5-31. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYQQGroupModel.h" #import "YYfriendCell.h" #import "YYFriendsModel.h "@interface Yyviewcontroller () 15/**16 * is used to save all grouped data */18 @property (nonatomic,strong) Nsarray *groupfriends;19 @end20 @implementation YYViewController22 #pragma mark-lazy load 23//1. Get the data first, and implement the lazy load-(Nsarray *) GROUPFRIENDS25 { Groupfriends==nil) {nsstring *fullpath=[[nsbundle mainbundle]pathforresource:@ "Friends.plist" ofType:nil];28 Nsarray *arraym=[nsarray arraywithcontentsoffile:fullpath];29 Nsmutablearray *models=[nsmutablear Ray arraywithcapacity:arraym.count];31 for (nsdictionary *dict in Arraym) {Yyqqgroupmodel *group=[y Yqqgroupmodel qqgroupmodelwithdict:dict];33 [Models addobject:group];34}35 _groupfriends=[models copy];36}37 return _groupfriends;38}39-(void) viewDidLoad41 {[super viewdidload];43 Sel F.tableview.sectionheaderheight = 100;44}45 #pragma mark-Set Data source 47//Return how many groups 48//Why not smart tips here? Because these methods are in the UITableView protocol, the default does not adhere to the protocol, so that the host controller class inherits Uitableviewcontroller, has complied with the (Nsinteger) Numberofsectionsintableview: (UITableView *) tableView50 {self.groupfriends.count;52}53//Each group returns how many rows-(nsinte GER) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) Section55 {56//Remove corresponding group model Yyqqgroupmodel *GROUP=SELF.GROUPFRIENDS[SECTION];58//Returns the number of friends in the corresponding group. Group.friends.count;60}61/per group per row (Uitableviewcel L *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath63 {64//1. Create CELL65 YYFRIENDC Ell *cell=[yyfriendcell cellwithtableview:tableview];66 67//2. Setting cell68 Yyqqgroupmodel *group=self.groupfriends[in dexpath.section];69 Yyfriendsmodel *friends=group.friends[indexpath.row];70 Cell.Friends=friends;71//3. Returns a cell72 return cell;73}74 #pragma mark-Proxy method 77//When a group header enters the field of view, the method is called by-(UIV Iew *) TableView: (UITableView *) TableView viewforheaderinsection: (Nsinteger) section79 {80//1. Create head view Bayi UIView * view = [[UIView alloc] init];82 view.backgroundcolor = [Uicolor graycolor];83//2. Return to head view + return view;85 }86 87//Set height of group header title (cgfloat) TableView: (UITableView *) TableView heightforheaderinsection: (Nsinteger) section89 { 44;91}92 #pragma mark hides the status bar 94-(BOOL) PrefersStatusBarHidden95 {$ return yes;97}98 @end
The humble effect achieved:
Third, the attention point
(1) How to set the head view
(2) When overriding the set method, the rollback should be taken into account.
iOS development UI chapter-using UITableView to complete a simple QQ friends List (i)