美文网首页iOS Developer
54、[ iOS ] TableView 模块化

54、[ iOS ] TableView 模块化

作者: 天听云道 | 来源:发表于2017-04-21 15:29 被阅读163次

UITableView 是 iOS 开发中最常用的控件之一,我们对其进行模块抽象化,使其用起来更加方便,简单。

Demo示例:CHTableView

一、用法简介

1、引入 demo 中的 CHTableView 文件夹及其中所有类
2、导入 "CHCommonTableData.h","CHCommonTableDelegate.h",
3、声明 @property (nonatomic ,strong) CHCommonTableDelegate *delegator;
4、viewDidLoad 调用

[self buildData];

// --必须写在给 CHTableView 的 delegate 和 dataSource 赋值之前
__weak typeof (self) weakSelf = self; // - block块,防止循环引用
self.delegator = [[CHCommonTableDelegate alloc] initWithTableData:^NSArray *{
return weakSelf.data;
}];

// --创建初始化tableview
self.CHTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.CHTableView.backgroundColor = [UIColor whiteColor];
self.CHTableView.delegate        = self.delegator;
self.CHTableView.dataSource      = self.delegator;
self.CHTableView.tableFooterView = [[UIView alloc] init]; // - 不显示空白cell
[self.view addSubview:self.CHTableView];

5、buildData 方法

    NSArray *data = @[
                      @{
                          HeaderTitle:@"",
                          HeaderHeight:@(20),
                          RowContent :@[
                                  @{
                                      Title         : @"个人简介",
                                      ExtraInfo     : @"136********",
                                      CellClass     : @"CHTestTableViewCell",
                                      RowHeight     : @(100),
                                      CellAction    : @"onTouchPortrait:",
                                      ShowAccessory : @(YES)
                                      },
                                  ],
                          FooterTitle:@"",
                          FooterHeight:@(20)
                          },
                      @{
                          HeaderTitle:@"",
                          HeaderHeight:@(20),
                          RowContent :@[
                                  @{
                                      Title      :@"昵称",
                                      DetailTitle:@"HaoTime",
                                      CellAction :@"onTouchNickSetting:",
                                      RowHeight     : @(50),
                                      ShowAccessory : @(YES),
                                      },
                                  @{
                                      Title      :@"性别",
                                      DetailTitle:@"男",
                                      CellAction :@"onTouchGenderSetting:",
                                      RowHeight     : @(50),
                                      ShowAccessory : @(YES)
                                      },
                                  @{
                                      Title      :@"生日",
                                      DetailTitle:@"保密",
                                      CellAction :@"onTouchBirthSetting:",
                                      RowHeight     : @(50),
                                      ShowAccessory : @(YES)
                                      },
                                  @{
                                      Title      :@"手机",
                                      DetailTitle:@"136********",
                                      CellAction :@"onTouchTelSetting:",
                                      RowHeight     : @(50),
                                      ShowAccessory : @(YES)
                                      },
                                  @{
                                      Title      :@"邮箱",
                                      DetailTitle:@"124****587@qq.com",
                                      CellAction :@"onTouchEmailSetting:",
                                      RowHeight     : @(50),
                                      ShowAccessory : @(YES)
                                      },
                                  @{
                                      Title      :@"签名",
                                      DetailTitle:@"哎呦不错哦",
                                      CellAction :@"onTouchSignSetting:",
                                      RowHeight     : @(50),
                                      ShowAccessory : @(YES)
                                      },
                                  ],
                          FooterTitle:@"",
                          FooterHeight:@(20)
                          },
                      ];

self.data = [CHCommonTableSection sectionsWithData:data];

二、效果图

相关文章

网友评论

    本文标题:54、[ iOS ] TableView 模块化

    本文链接:https://www.haomeiwen.com/subject/wastyttx.html