本系列要示範如何以 UITableView 完成一個可以展開、收攏分組的簡易 QQ 好友列表。第一篇先說明專案結構與資料來源,再逐步帶出基礎程式碼。
資料以 friends.plist 存放,透過懶加載在需要時才讀取,並把字典轉成模型物件,是這個範例的核心資料流。
主控制器直接繼承自 UITableViewController:
// YYViewController.h #import@interface YYViewController : UITableViewController @end
並在 storyboard 中完成關聯。
YYQQGroupModel.h:
#import@interface YYQQGroupModel : NSObject /** 名稱屬性 */ @property (nonatomic, copy) NSString *name; /** 是否在線 */ @property (nonatomic, copy) NSString *online; /** 好友列表 */ @property (nonatomic, strong) NSArray *friends; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)qqGroupModelWithDict:(NSDictionary *)dict; @end
YYQQGroupModel.m:
#import "YYQQGroupModel.h"
#import "YYFriendsModel.h"
@implementation YYQQGroupModel
-(instancetype)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
NSMutableArray *models = [NSMutableArray arrayWithCapacity:self.friends.count];
for (NSDictionary *dict in self.friends) {
YYFriendsModel *friends = [YYFriendsModel friendsWithDict:dict];
[models addObject:friends];
}
_friends = [models copy];
}
return self;
}
+(instancetype)qqGroupModelWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@endYYFriendsModel.h:
#import@interface YYFriendsModel : NSObject /** 每個好友的名稱 */ @property (nonatomic, copy) NSString *name; /** 每個好友的頭像 */ @property (nonatomic, copy) NSString *icon; /** 每個好友的個性簽名 */ @property (nonatomic, copy) NSString *intro; /** 該好友是否是 VIP */ @property (nonatomic, assign, getter = isVip) BOOL vip; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)friendsWithDict:(NSDictionary *)dict; @end
YYFriendsModel.m:
#import "YYFriendsModel.h"
@implementation YYFriendsModel
-(instancetype)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)friendsWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@endYYfriendCell.h:
#import@class YYFriendsModel; @interface YYfriendCell : UITableViewCell @property (nonatomic, strong) YYFriendsModel *friends; +(instancetype)cellWithTableview:(UITableView *)tableView; @end
YYfriendCell.m:
#import "YYfriendCell.h"
#import "YYFriendsModel.h"
@interface YYfriendCell ()
@end
@implementation YYfriendCell
+(YYfriendCell *)cellWithTableview:(UITableView *)tableView {
static NSString *identifier = @"qq";
YYfriendCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[YYfriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
NSLog(@"創建一個 cell");
}
return cell;
}
-(void)setFriends:(YYFriendsModel *)friends {
_friends = friends;
self.imageView.image = [UIImage imageNamed:_friends.icon];
self.textLabel.text = _friends.name;
self.detailTextLabel.text = _friends.intro;
// 注意點:如果不寫 else 設為黑色,會怎樣?
if (_friends.isVip) {
[self.textLabel setTextColor:[UIColor redColor]];
} else {
[self.textLabel setTextColor:[UIColor blackColor]];
}
}
@endYYViewController.m:
#import "YYViewController.h"
#import "YYQQGroupModel.h"
#import "YYfriendCell.h"
#import "YYFriendsModel.h"
@interface YYViewController ()
/** 用來保存所有的分組數據 */
@property (nonatomic, strong) NSArray *groupFriends;
@end
@implementation YYViewController
#pragma mark - 懶加載
-(NSArray *)groupFriends {
if (_groupFriends == nil) {
NSString *fullpath = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
NSArray *arrayM = [NSArray arrayWithContentsOfFile:fullpath];
NSMutableArray *models = [NSMutableArray arrayWithCapacity:arrayM.count];
for (NSDictionary *dict in arrayM) {
YYQQGroupModel *group = [YYQQGroupModel qqGroupModelWithDict:dict];
[models addObject:group];
}
_groupFriends = [models copy];
}
return _groupFriends;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.sectionHeaderHeight = 100;
}
#pragma mark - 設置數據源
// 為什麼這裡沒有智慧提示?因為這些方法屬於 UITableView 協議,
// 當主控制器繼承 UITableViewController 後就已遵守協議
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.groupFriends.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
YYQQGroupModel *group = self.groupFriends[section];
return group.friends.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YYfriendCell *cell = [YYfriendCell cellWithTableview:tableView];
YYQQGroupModel *group = self.groupFriends[indexPath.section];
YYFriendsModel *friends = group.friends[indexPath.row];
cell.friends = friends;
return cell;
}
#pragma mark - 代理方法
// 分組標題進入視野時呼叫
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor grayColor];
return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
#pragma mark 隱藏狀態欄
-(BOOL)prefersStatusBarHidden {
return YES;
}
@end這一篇先建立好資料模型、Cell 與最基礎的分組列表;下一篇再進一步加入可折疊的自訂 Header 與點擊展開效果。