IOS development UI-use UItableview to complete a simple QQ friends list (1)

Source: Internet
Author: User

IOS development UI-use UItableview to complete a simple QQ friend list (1) I. project structure and plist file II. Implementation Code 1. note: The master controller directly inherits the UITableViewController to copy the code // YYViewController. h // 02-QQ friends list (loading basic data) /// Created by apple on 14-5-31. // Copyright (c) 2014 itcase. all rights reserved. // # import <UIKit/UIKit. h> @ interface YYViewController: UITableViewController @ end the copy code is associated in storyboard. code Data Model section: YYQQGroupModel. hfile copy code 1 // 2 // YYQQGroupModel. h 3 // 02-QQ friends list (basic data loading) 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 11 @ interface YYQQGroupModel: NSObject12/** 13 * name attribute 14 */15 @ property (nonatomic, copy) NSString * name; 16/** 17 * online? 18 */19 @ property (nonatomic, copy) NSString * online; 20/** 21 * friends list 22 */23 @ property (nonatomic, strong) NSArray * friends; 24 25 -( Instancetype) initWithDict :( NSDictionary *) dict; 26 + (instancetype) qqGroupModelWithDict :( NSDictionary *) dict; 27 @ end copy the code YYQQGroupModel. copy the m file to code 1 // 2 // YYQQGroupModel. m 3 // 02-QQ friends list (basic data loading) 4 // 5 // Created by apple on 14-5-31. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYQQGroupModel. h "10 # import" YYFriendsModel. h "11 12 @ implementation YYQQGroupModel13-(instance Type) initWithDict :( NSDictionary *) dict14 {15 if (self = [super init]) {16 // convert the dictionary to model 17 [self setValuesForKeysWithDictionary: dict]; 18 19 // define an array to save the converted model 20 NSMutableArray * models = [NSMutableArray arrayWithCapacity: self. friends. count]; 21 for (NSDictionary * dict in self. friends) {22 YYFriendsModel * friends = [YYFriendsModel friendsWithDict: dict]; 23 [models addObject: friends]; 24} 25 _ friends = [models copy]; 26} 27 return self; 28} 29 30 + (instancetype) qqGroupModelWithDict :( NSDictionary *) dict31 {32 return [[self alloc] initWithDict: dict]; 33} 34 @ end copy the code YYFriendsModel. h file copy code 1 // 2 // YYFriendsModel. h 3 // 02-QQ friends list (basic data loading) 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 11 @ interface YYFriendsModel: NSObject1 2/** 13 * name of each friend 14 */15 @ property (nonatomic, copy) NSString * name; 16/** 17 * The profile picture of each friend is 18 */19 @ property (nonatomic, copy) NSString * icon; 20/** 21 * Each Friend's personalized signature 22 */23 @ property (nonatomic, copy) NSString * intro; 24/** 25 * Whether the friend is vip26 */27 @ property (nonatomic, assign, getter = isVip) BOOL vip; 28 29-(instancetype) initWithDict :( NSDictionary *) dict; 30 + (instancetype) friendsWithDict :( NSDictionary *) dict; 31 @ end copy code Y YFriendsModel. m file copy code 1 // 2 // YYFriendsModel. m 3 // 02-QQ friends list (basic data loading) 4 // 5 // Created by apple on 14-5-31. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYFriendsModel. h "10 11 @ implementation YYFriendsModel12-(instancetype) initWithDict :( NSDictionary *) dict13 {14 if (self = [super init]) {15 [self setValuesForKeysWithDictionary: dict]; 16} 17 return self; 18} 19 20 + (ins Tancetype) friendsWithDict :( NSDictionary *) dict21 {22 return [[self alloc] initWithDict: dict]; 23} 24 @ end copy the Code view part of YYfriendCell. h file copy code 1 // 2 // YYfriendCell. h 3 // 02-QQ friends list (basic data loading) 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 13 @ property (nonatom Ic, strong) YYFriendsModel * friends; 14 15 + (instancetype) cellWithTableview :( UITableView *) tableView; 16 @ end copy code YYfriendCell. m file copy code 1 // 2 // YYfriendCell. m 3 // 02-QQ friends list (basic data loading) 4 // 5 // Created by apple on 14-5-31. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYfriendCell. h "10 # import" YYFriendsModel. h "11 // Private extension 12 @ interface YYfriendCell () 13 14 15 @ end16 @ implementat Ion YYfriendCell17 18 + (YYfriendCell *) cellWithTableview :( UITableView *) tableView19 {20 static NSString * identifier = @ "qq"; 21 YYfriendCell * cell = [tableView identifier: identifier]; 22 if (cell = nil) {23 // here use the built-in 24 cell style = [[YYfriendCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifier]; 25 NSLog (@ "Create a cell"); 26} 27 return cell; 28} 29 30-(void) setFri Ends :( YYFriendsModel *) friends31 {32 _ friends = friends; 33 // 1. set Avatar 34 self. imageView. image = [UIImage imageNamed: _ friends. icon]; 35 // 2. set the nickname 36 self. textLabel. text = _ friends. name; 37 // 3. set overview 38 self. detailTextLabel. text = _ friends. intro; 39 // determine whether it is a member 40/** 41 * here is a note. What if else is set to Black without writing? 42 */43 if (_ friends. isVip) {44 [self. textLabel setTextColor: [UIColor redColor]; 45} else46 {47 [self. textLabel setTextColor: [UIColor blackColor]; 48} 49} 50 @ end copy part of the main controller of the Code YYViewController. m file copy code 1 // 2 // YYViewController. m 3 // 02-QQ friends list (basic data loading) 4 // 5 // Created by apple on 14-5-31. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 # import" YYQQGro UpModel. h "11 # import" YYfriendCell. h "12 # import" YYFriendsModel. h "13 14 @ interface YYViewController () 15/** 16 * is used to save all group data 17 */18 @ property (nonatomic, strong) NSArray * groupFriends; 19 @ end20 21 @ implementation YYViewController22 # pragma mark-lazy loading 23 // 1. get the data first, and implement lazy loading of 24-(NSArray *) groupFriends25 {26 if (_ groupFriends = nil) {27 NSString * fullpath = [[NSBundle mainBundle] pathForResource: @ "friends. plist "o FType: nil]; 28 NSArray * arrayM = [NSArray arrayWithContentsOfFile: fullpath]; 29 30 NSMutableArray * models = [NSMutableArray arrayWithCapacity: arrayM. count]; 31 for (NSDictionary * dict in arrayM) {32 YYQQGroupModel * group = [YYQQGroupModel qqGroupModelWithDict: dict]; 33 [models addObject: group]; 34} 35 _ groupFriends = [models copy]; 36} 37 return _ groupFriends; 38} 39 40-(void) viewDidLoad41 {42 [super viewDidLo Ad]; 43 self. tableView. sectionHeaderHeight = 100; 44} 45 46 # pragma mark-set the data source 47 // how many groups are returned 48 // Why is there no smart prompt here? Because these methods are in the uitableview protocol and do not comply with the Protocol by default, after the master controller class inherits uitableviewcontroller, it has followed the 49-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView50 {51 return self. groupFriends. count; 52} 53 // The number of rows returned in each group 54-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section55 {56 // retrieve the corresponding group model 57 YYQQGroupModel * group = self. groupFriends [section]; 58 // return the number of friends in the corresponding group 59 return group. friends. count; 60} 61 // the content of each row in each group 62-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath63 {64 // 1. create cell65 YYfriendCell * cell = [YYfriendCell cellWithTableview: tableView]; 66 67 // 2. set cell68 YYQQGroupModel * group = self. groupFriends [indexPath. section]; 69 YYFriendsModel * friends = group. friends [indexPath. row]; 70 cell. friends = friends; 71 // 3. return a cell72 return cell; 73} 74 75 76 # pragma mark-proxy Method 77 // this method is called when a group title enters the field of view (UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section79 {80 // 1. create a header view 81 UIView * view = [[UIView alloc] init]; 82 view. backgroundColor = [UIColor grayColor]; 83 // 2. return to the header view 84 return view; 85} 86 87 // set the height of the group header title 88-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section89 {90 return 44; 91} 92 93 # pragma mark hide status bar 94-(BOOL) prefersStatusBarHidden95 {96 return YES; 97} 98 @ end

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.