0、QQ好友列表实现
1、实现数据源方法
#pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 10; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { SLQFriendGroup *group = self.friendList[section]; return group.friends.count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SLQFriendCell *cell = [SLQFriendCell cellWithTableView:tableView]; return cell; }
|
2、模型定义
- 2.0、在控制器定义个数组,存放解析的plist数据
/**朋友数组*/
@property (nonatomic, copy) NSArray *friendList;
@class SLQFriend; @interface SLQFriendGroup : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int online;
@property (nonatomic, copy) NSArray *friends;
@property (nonatomic, assign,getter=isOpened) BOOL opened;
- (instancetype)initWithDictionary:(NSDictionary *)dict; + (instancetype)FriendGroupWithDictionary:(NSDictionary *)dict; @end
#import "SLQFriendGroup.h" #import "SLQFriend.h"
@implementation SLQFriendGroup
- (instancetype)initWithDictionary:(NSDictionary *)dict { if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; NSMutableArray *groupFriends = [NSMutableArray array]; for (NSDictionary *dict in self.friends) { SLQFriend *friend = [SLQFriend friendWithDict:dict]; [groupFriends addObject:friend]; } self.friends = groupFriends; } return self; } + (instancetype)FriendGroupWithDictionary:(NSDictionary *)dict { return [[self alloc] initWithDictionary:dict]; } @end ``` - 2.2、朋友模型
```objc #import <Foundation/Foundation.h>
@interface SLQFriend : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *intro;
@property (nonatomic, assign, getter=isVip) BOOL vip;
- (instancetype)initFriendWithDict:(NSDictionary *)dict; + (instancetype)friendWithDict:(NSDictionary *)dict; @end
@implementation SLQFriend
- (instancetype)initFriendWithDict:(NSDictionary *)dict { if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; }
+ (instancetype)friendWithDict:(NSDictionary *)dict { return [[self alloc] initFriendWithDict:dict]; } @end
|
3、字典转模型
#pragma mark - 懒加载 - (NSArray *)friendList { if (!_friendList) { _friendList = [NSArray array]; NSString *path = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil]; NSArray *friends = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *mutableFriends = [NSMutableArray array]; for (NSDictionary *dict in friends) { SLQFriendGroup *group = [SLQFriendGroup FriendGroupWithDictionary:dict]; [mutableFriends addObject:group]; } _friendList = mutableFriends; } return _friendList; }
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.friendList.count; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { SLQFriendGroup *group = self.friendList[section]; return group.isOpened ? group.friends.count : 0; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SLQFriendCell *cell = [SLQFriendCell cellWithTableView:tableView]; SLQFriendGroup *group = self.friendList[indexPath.section]; cell.Friend = group.friends[indexPath.row]; return cell; }
|
4、自定义cell
#import <UIKit/UIKit.h> @class SLQFriend;
@interface SLQFriendCell : UITableViewCell
@property (nonatomic, strong) SLQFriend *Friend;
+ (SLQFriendCell *)cellWithTableView:(UITableView *)tableView;
@end
#import "SLQFriendCell.H" #import "SLQFriendGroup.h" #import "SLQFriend.h"
@implementation SLQFriendCell
- (void)setFriend:(SLQFriend *)Friend { _Friend = Friend; self.textLabel.text = Friend.name; self.detailTextLabel.text = Friend.intro; self.imageView.image = [UIImage imageNamed:Friend.icon]; self.textLabel.textColor = Friend.isVip ? [UIColor redColor] : [UIColor blackColor]; }
+ (SLQFriendCell *)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"Cell"; SLQFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[SLQFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } return cell; }
|

#import <UIKit/UIKit.h> @class SLQFriendGroup; @interface SLQHeader : UITableViewHeaderFooterView
@property (nonatomic, strong) SLQFriendGroup *friendGroup;
@property (nonatomic, weak) UIButton *contentButton;
@property (nonatomic, weak) UILabel *onlineLabel; + (instancetype)headerWithTableView:(UITableView *)tableView; @end
|
- (void)setFriendGroup:(SLQFriendGroup *)friendGroup { _friendGroup = friendGroup; NSString *name = [NSString stringWithFormat:@" %@",friendGroup.name]; [self.contentButton setTitle:name forState:UIControlStateNormal]; [self.contentButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; self.onlineLabel.text = [NSString stringWithFormat:@"%zd/%zd",friendGroup.online,friendGroup.friends.count]; }
|
- 5.1、封装header生成操作,返回header
+ (instancetype)headerWithTableView:(UITableView *)tableView { static NSString *ID = @"header"; SLQHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID]; if (header == nil) { header = [[SLQHeader alloc] initWithReuseIdentifier:ID]; } return header; }
|
- (void)layoutSubviews { [super layoutSubviews]; self.contentButton.frame = self.bounds; self.contentButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); NSInteger countWidth = 150; NSInteger countHeight = self.bounds.size.height; NSInteger countX = self.bounds.size.width - 10 - countWidth; NSInteger countY = 0; self.onlineLabel.frame = CGRectMake(countX, countY, countWidth, countHeight); CGFloat rotation = self.friendGroup.isOpened? M_PI_2 : 0; self.contentButton.imageView.transform = CGAffineTransformMakeRotation(rotation); } ```
- 5.3、自定义`initWithReuseIdentifier:ID`方法,添加自定义控件
```objc
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithReuseIdentifier:reuseIdentifier]) { UIButton *contentButton = [[UIButton alloc] init]; [self.contentView addSubview:contentButton]; [contentButton setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal]; [contentButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal]; [contentButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted]; contentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [contentButton addTarget:self action:@selector(contentButtonClick:) forControlEvents:UIControlEventTouchUpInside]; _contentButton = contentButton; UILabel *onlineLabel = [[UILabel alloc] init]; [self.contentView addSubview:onlineLabel]; [onlineLabel setTextAlignment:NSTextAlignmentRight]; _onlineLabel = onlineLabel; } return self; }
|
- 5.4、响应header点击事件,点击过后展开或者收缩内部cell,这个牵涉到tableView的数据更新,那么可以将这个事件传递出去(使用代理)
- (void)contentButtonClick:(UIButton *)btn { self.friendGroup.opened = !self.friendGroup.isOpened; if ([self.delegate respondsToSelector:@selector(headerDidClicked:)]) { [self.delegate headerDidClicked:self]; } }
|
- 5.5、代理实现,只需实现一个方法就行,而且是可选的
@class SLQHeader; @protocol SLQHeaderDelegate <NSObject>
@optional - (void)headerDidClicked:(SLQHeader *)header;
@end
@interface SLQHeader : UITableViewHeaderFooterView
@property (nonatomic, strong) SLQFriendGroup *friendGroup;
@property (nonatomic, weak) UIButton *contentButton;
@property (nonatomic, weak) UILabel *onlineLabel;
@property (nonatomic, weak) id<SLQHeaderDelegate> delegate; + (instancetype)headerWithTableView:(UITableView *)tableView;
@end
|
#pragma mark - UITableViewDelegate - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { SLQHeader *header = [SLQHeader headerWithTableView:tableView]; SLQFriendGroup *group = self.friendList[section]; header.friendGroup = group; header.delegate = self; return header; }
- (void)headerDidClicked:(SLQHeader *)header { [self.tableView reloadData]; }
|
