github 地址:https://github.com/pengGitHubUp/GPTableViewKit
一:控制器代码
@interface DayMyTrainLibListVC ()<GPTableViewManagerDelegate>//声明代理
/** 表格 */
@property (nonatomic, strong) UITableView *tableViewList;
/** 表格控制 */
@property (nonatomic, strong) GPTableViewManager *tableViewManager;
/** 页数 */
@property (nonatomic, assign) NSInteger page;
/** server */
@property (nonatomic, strong) ServerManager *serverList;
/** 列表 */
@property (nonatomic, strong) NSMutableArray *projectList;
@end
@implementation DayMyTrainLibListVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"我的锻炼方案";
[self.view addSubview:self.tableViewList];
[self makeConstraint];
[self requestForProject];
}
/** table */
- (UITableView *)tableViewList {
if (!_tableViewList) {
_tableViewList = [[UITableView alloc] init];
_tableViewList.showsVerticalScrollIndicator = YES;
_tableViewList.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_tableViewList.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableViewList.backgroundColor = COLOR_BACKGROUND;
}
return _tableViewList;
}
/** 布局 */
- (void)makeConstraint {
WS(ws);
[_tableViewList mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(ws.view);
make.right.equalTo(ws.view);
make.top.equalTo(ws.navigationView.mas_bottom);
make.bottom.equalTo(ws.view);
}];
}
/** 管理类 */
- (GPTableViewManager *)tableViewManager {
if (!_tableViewManager) {
//注意懒加载写法(self.)
_tableViewManager = [[GPTableViewManager alloc] initWithTableView:self.tableViewList delegate:self];
}
return _tableViewManager;
}
//manager delegate
- (void)tableViewDataSource:(GPTableViewManager *)manager; {
// 为了避免重复添加,remove一下
[manager removeAllSections];
//添加section:方案列表
[manager addSection:[self sectionMyList]];
}
/** 实线一个section */
- (GPTableViewSectionManager *)sectionMyList {
GPTableViewSectionManager *section = [GPTableViewSectionManager section];
NSInteger i = 0;
WS(ws);
for (DayProjectListDetailModel *detail in self.projectList) {
GPTableViewRowManager *row = [GPTableViewRowManager row];
row.rowHeight = 78;
row.cellName = @"DayMyProjectListTableViewCell";
row.model = detail;
row.didSelectRowAtIndex = ^(GPTableViewRowManager *row, NSInteger index) {
// 回调处理,如果没有回调可以处理,就不写;另外GPTableViewRowManager里面有好多种回调,根据自己的需求,自行选择
};
[section addRow:row];
i ++;
// 划分割线
if (i != _projectList.count) {
// 扩展的一个便利方法,添加分割线,其实也是一个cell
[section addSeparateRowHeight:0.5 color:COLOR_LINE leftMargin:16 rightMargin:0];
}
}
return section;
}
二、cell代码举例
@interface DayMyProjectListTableViewCell ()
/** 项目名 */
@property (nonatomic, strong) UILabel *projectNameLabel;
@end
@implementation DayMyProjectListTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.projectNameLabel];
[self makeConstraints];
WS(ws);
[self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
// 点击cell回调给控制器
if (ws.row.didSelectRowAtIndex) {
ws.row.didSelectRowAtIndex(ws.row, 1);
}
}]];
}
return self;
}
// 父类方法,每次滑动或者刷新都会走
- (void)cellWillAppear:(id)model {
if ([model isKindOfClass:[DayProjectListDetailModel class]]) {
DayProjectListDetailModel *detail = model;
_projectNameLabel.text = detail.projectName;
}
}
// 布局
- (void)makeConstraints {
WS(ws);
[_projectNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(ws.contentView).offset(16);
make.top.equalTo(ws.contentView).offset(16);
}];
}
/** 懒加载一个label */
- (UILabel *)projectNameLabel {
if (!_projectNameLabel) {
_projectNameLabel = [[UILabel alloc] init];
_projectNameLabel.font = [UIFont boldSystemFontOfSize:18];
_projectNameLabel.textAlignment = NSTextAlignmentLeft;
_projectNameLabel.textColor = COLOR_BLACK4;
}
return _projectNameLabel;
}
注:上面的代码是自己用的,仅仅是作为连接使用,大家忽视即可