上一篇简述了UITableView的使用,本篇基于MVC模式继续练习使用UITableView。
UITableView具体属性及方法可以参考文章:UITableView简介
UITableView简单使用可以参考文章:UITableView使用练习
MVC设计模式简介可以参考文章:MVC设计模式简介
写了个简单的例子,控制器继承UITableViewController,不需要再去手动添加协议,设置代理。遵循MVC设计模式,自定义头部视图、Cell、Model数据模型效果如图32-1:
下面贴上代码:
HWTableViewController:
#import
@interface HWTableViewController : UITableViewController
@end
#import "HWTableViewController.h"
#import "HWHeaderView.h"
#import "HWTableViewCell.h"
#import "HWCellModel.h"
@interface HWTableViewController ()
@property (nonatomic, strong) NSMutableArray *HWInfoArray;
@end
@implementation HWTableViewController
- (NSMutableArray *)HWInfoArray
{
if (_HWInfoArray == nil) {
_HWInfoArray = [NSMutableArray array];
}
return _HWInfoArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
//创建头部视图
self.tableView.tableHeaderView = [[HWHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
//获取数据
[self getInfo];
}
- (void)getInfo
{
//实际开发数据是网络获取到的,这里模拟给出一个数据
NSArray *array = @[@[@{@"image" : @"hero", @"title" : @"标题1", @"subTitle" : @"副标题1"},
@{@"image" : @"hero", @"title" : @"标题2", @"subTitle" : @"副标题2"}],
@[@{@"image" : @"hero", @"title" : @"标题3", @"subTitle" : @"副标题3"},
@{@"image" : @"hero", @"title" : @"标题4", @"subTitle" : @"副标题4"},
@{@"image" : @"hero", @"title" : @"标题5", @"subTitle" : @"副标题5"},
@{@"image" : @"hero", @"title" : @"标题6", @"subTitle" : @"副标题6"},
@{@"image" : @"hero", @"title" : @"标题7", @"subTitle" : @"副标题7"}]];
//解析数据,转模型保存
for (int i = 0; i < array.count; i++) {
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in array[i]) {
[tempArray addObject:[HWCellModel HWInfoWithDictionary:dict]];
}
[self.HWInfoArray addObject:tempArray];
}
}
#pragma mark - Table view data source
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.HWInfoArray.count;
}
//组中行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.HWInfoArray[section] count];
}
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HWTableViewCell *cell = [HWTableViewCell cellWIthTableView:tableView];
cell.model = self.HWInfoArray[indexPath.section][indexPath.row];
return cell;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取到当前被点击的cell
HWTableViewCell *cell = (HWTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *str = [NSString stringWithFormat:@"点击了第%ld组第%ld行", indexPath.section, indexPath.row];
cell.lable.text = str;
}
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70.0f;
}
//设置头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"第一组";
}
return @"第二组";
}
//设置尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"这是尾部标题";
}
@end
HWHeaderView:
#import
@interface HWHeaderView : UIView
@end
#import "HWHeaderView.h"
@implementation HWHeaderView
//重写init方法
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
//创建一个标签作为头部视图
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 100)];
label.text = @"这是头部视图";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor grayColor];
[self addSubview:label];
}
return self;
}
@end
HWTableViewCell:
#import
@class HWCellModel;
@interface HWTableViewCell : UITableViewCell
@property (nonatomic, weak) UILabel *lable;
@property (nonatomic, strong) HWCellModel *model;
+ (instancetype)cellWIthTableView:(UITableView *)tableView;
@end
#import "HWTableViewCell.h"
#import "HWCellModel.h"
@interface HWTableViewCell ()
@property (nonatomic, weak) UIImageView *imgView;
@property (nonatomic, weak) UILabel *subLabel;
@end
@implementation HWTableViewCell
+ (instancetype)cellWIthTableView:(UITableView *)tableView
{
//cell复用,唯一标识
static NSString *identifier = @"HWCell";
//先在缓存池中取
HWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//缓存池中没有再创建,并添加标识,cell移出屏幕时放入缓存池以复用
if (cell == nil) {
cell = [[HWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
//重写init方法构建cell内容
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 80, 60)];
[self.contentView addSubview:imageView];
self.imgView = imageView;
//标题
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 15, 200, 20)];
label.font = [UIFont systemFontOfSize:20.0f];
[self.contentView addSubview:label];
self.lable = label;
//副标题
UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 40, 200, 13)];
subLabel.font = [UIFont systemFontOfSize:13.0f];
[self.contentView addSubview:subLabel];
self.subLabel = subLabel;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
//重写set方法,模型传递
- (void)setModel:(HWCellModel *)model
{
_model = model;
self.imgView.image = [UIImage imageNamed:model.image];
self.lable.text = model.title;
self.subLabel.text = model.subTitle;
}
@end
HWCellModel:
#import
@interface HWCellModel : NSObject
@property (nonatomic, copy) NSString *image;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
- (id)initWithDictionary:(NSDictionary *)dict;
+ (id)HWInfoWithDictionary:(NSDictionary *)dict;
@end
#import "HWCellModel.h"
@implementation HWCellModel
- (id)initWithDictionary:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (id)HWInfoWithDictionary:(NSDictionary *)dict
{
return [[self alloc] initWithDictionary:dict];
}
@end