tableView for TTXS

本文介绍了一个基于 GPTableViewKit 的 iOS 应用实现方案列表的示例。通过自定义控制器和单元格,实现了数据展示及交互功能。文章详细展示了如何初始化控制器、设置表格视图、配置单元格以及实现数据源等。

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;

}

 

注:上面的代码是自己用的,仅仅是作为连接使用,大家忽视即可

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值