如何写静态tableView页面
来源:互联网 发布:二维编程雕刻机 编辑:程序博客网 时间:2024/05/27 20:34
学习了这篇文章 架构篇-谈谈如何写个人中心这类页面(静态tableView页面的编写)
给自己的学习做个记录。
根据自己的理解重写了一部分代码。
- WynStaticCell.h
#import "WynBaseCell.h"@class BaseCellDescriber;@interface WynBaseStaticCell : WynBaseCell@property (nonatomic, strong) BaseCellDescriber *describer;@endstatic NSString * const kUITableViewCell = @"WynBaseStaticCell";static NSString * const kBaseCell = @"WynBaseStaticCell";typedef void (^CustomCellBlock)(WynBaseStaticCell *cell, BaseCellDescriber *describer);@interface BaseCellDescriber : NSObject/** 判断Cell 由代码还是xib创建 */@property (nonatomic, assign) CellCreationType cellCreationType;@property (nonatomic, copy) NSString *identifier;/** UITableView default value */@property (nonatomic, copy) NSString *textLabel_text;@property (nonatomic, strong) UIFont *textLabel_font;@property (nonatomic, strong) UIColor *textLabel_textColor;@property (nonatomic, copy) NSString *detailTextLabel_text;@property (nonatomic, strong) UIFont *detailTextLabel_font;@property (nonatomic, strong) UIColor *detailTextLabel_textColor;@property (nonatomic, assign) UITableViewCellAccessoryType accessoryType;/** Block */// (可选)在block中对cell赋值@property (nonatomic, copy) CustomCellBlock customCellBlock;@property (nonatomic, copy) void(^selectCellBlock)(WynBaseStaticCell *cell, BaseCellDescriber *describer);- (CGFloat)cellHeight;- (CustomCellBlock)defaultCustomCellBlock;@end
- WynStaticCell.m
#import "WynStaticCell.h"static const CGFloat defaultCellHeight = 44.0;@implementation WynBaseStaticCell#pragma mark - Life cycle./*- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { } return self;} */- (CGSize)sizeThatFits:(CGSize)size { return CGSizeMake(0, defaultCellHeight);}- (void)setDescriber:(BaseCellDescriber *)describer { if (describer.identifier == kBaseCell) { self.textLabel.text = describer.textLabel_text; self.textLabel.font = describer.textLabel_font; self.textLabel.textColor = describer.textLabel_textColor; self.detailTextLabel.text = describer.detailTextLabel_text; self.detailTextLabel.font = describer.detailTextLabel_font; self.detailTextLabel.textColor = describer.detailTextLabel_textColor; self.accessoryType = describer.accessoryType; }}@end#pragma mark -@interface BaseCellDescriber ()@property (nonatomic, weak) WynBaseStaticCell *cell;@end@implementation BaseCellDescriber- (instancetype)init { if (self = [super init]) { _customCellBlock = [self defaultCustomCellBlock]; } return self;}- (CustomCellBlock)defaultCustomCellBlock { __weak typeof(self) wkSelf = self; return ^(WynBaseStaticCell *cell, BaseCellDescriber *describer) { __strong typeof(self) sgSelf = wkSelf; sgSelf.cell = cell; [cell setDescriber:describer]; };}- (CGFloat)cellHeight { return [_cell sizeThatFits:CGSizeZero].height;}@end
- WynStaticTableView.h
#import <UIKit/UIKit.h>#import "WynStaticCell.h"@interface WynStaticTableView : UITableView@property (nonatomic, assign) CGFloat headerHeight;/** 添加Describer @param block block @param section 需要添加到的section */- (void)addCellDescriber:(BaseCellDescriber *(^)())block inSection:(NSInteger)section;/** 一次性添加多个describers eg. @[@[describer1, describer2], @[describers3]] @param describers describers */- (void)loadCellDescribers:(NSArray<NSArray<BaseCellDescriber *> *> *)describers;/** 注册cell @param nibCells 存放 通过xib创建的cell类名 的数组 @param classCells 存放 通过代码创建的cell类名 的数组 */- (void)registNibCells:(NSArray<NSString *> *)nibCells classCells:(NSArray<NSString *> *)classCells;@end
- WynStaticTableView.m
#import "WynStaticTableView.h"@interface WynStaticTableView() <UITableViewDelegate, UITableViewDataSource>@property (nonatomic, strong) NSArray<NSArray<BaseCellDescriber *> *> *cellDescriptionDatas;@end@implementation WynStaticTableView- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{ self = [super initWithFrame:frame style:style]; if (self) { self.delegate = self; self.dataSource = self; _headerHeight = 0.1f; } return self;}- (void)addCellDescriber:(BaseCellDescriber *(^)())block inSection:(NSInteger)section { BaseCellDescriber *describer = block(); if(describer.cellCreationType == CellCreationType_code) { [self registerClass:[NSClassFromString(describer.identifier) class] forCellReuseIdentifier:describer.identifier]; } else if (describer.cellCreationType == CellCreationType_xib) { [self registerNib:[UINib nibWithNibName:describer.identifier bundle:nil] forCellReuseIdentifier:describer.identifier]; } [[self cellDescriptionDatasAtIndexCheck:section] addObject:describer];}- (NSMutableArray *)cellDescriptionDatasAtIndexCheck:(NSInteger)index { if (index == [self.cellDescriptionDatas count]) { NSMutableArray *arrM = [NSMutableArray array]; [self.cellDescriptionDatas addObject:arrM]; return arrM; } id value = [self.cellDescriptionDatas objectAtIndex:index]; if (value == [NSNull null]) { NSMutableArray *arrM = [NSMutableArray array]; [self.cellDescriptionDatas addObject:arrM]; return arrM; } return value;}- (void)loadCellDescribers:(NSArray<NSArray<BaseCellDescriber *> *> *)describers { _cellDescriptionDatas = [NSArray arrayWithArray:describers];}- (void)registNibCells:(NSArray<NSString *> *)nibCells classCells:(NSArray<NSString *> *)classCells { if (nibCells != nil) { for (NSString *cellString in nibCells) { // 注册 xib创建cell [self registerNib:[UINib nibWithNibName:cellString bundle:nil] forCellReuseIdentifier:cellString]; } } if (classCells != nil) { for (NSString *cellString in classCells) { // 注册 代码创建cell [self registerClass:[NSClassFromString(cellString) class] forCellReuseIdentifier:cellString]; } }}#pragma mark - UITableViewDataSource.- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _cellDescriptionDatas.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _cellDescriptionDatas[section].count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BaseCellDescriber *data = _cellDescriptionDatas[indexPath.section][indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:data.identifier]; data.customCellBlock((WynBaseStaticCell *)cell, data); return cell;}#pragma mark - UITableViewDelegate.- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return _headerHeight;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { BaseCellDescriber *data = _cellDescriptionDatas[indexPath.section][indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:data.identifier]; data.customCellBlock((WynBaseStaticCell *)cell, data); return [data cellHeight];}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; BaseCellDescriber *data = _cellDescriptionDatas[indexPath.section][indexPath.row]; if (data.selectCellBlock) { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; data.selectCellBlock((WynBaseStaticCell *)cell, data); }}@end
使用例子
- e.g.
- (void)viewDidLoad { [super viewDidLoad]; WynStaticTableView *tableView = [[WynStaticTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; tableView.backgroundColor = [UIColor lightTextColor]; tableView.headerHeight = 20.f; ...... BaseCellDescriber *cell0 = [BaseCellDescriber new]; cell0.identifier = kBaseCell; cell0.textLabel_text = @"UITableViewCell"; cell0.textLabel_textColor = [UIColor brownColor]; cell0.detailTextLabel_text = @"detailTextLabel_text"; cell0.detailTextLabel_textColor = [UIColor brownColor]; cell0.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell0.selectCellBlock = ^(WynBaseStaticCell *cell, BaseCellDescriber *describer) { NSLog(@"*=*=%s=*=*", __func__); }; [tableView loadCellDescribers:@[@[cell2Data, cell0], @[cell3Data]]]; [tableView registNibCells:nil classCells:@[ kUITableViewCell]]; [self.view addSubview:tableView]; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }];}
- e.g.
[tableView addCellDescriber:^BaseCellDescriber *{ MCDemoTableDescribeData *cell3Data = [[MCDemoTableDescribeData alloc] init]; cell3Data.identifier = @"MCDemoCell3"; cell3Data.indicateImageName = @"indicate"; cell3Data.title = @"cell3"; cell3Data.subTitle = @"cell3's subtitle"; return cell3Data; } inSection:0]; [tableView addCellDescriber:^BaseCellDescriber *{ BaseCellDescriber *cell0 = [BaseCellDescriber new]; cell0.identifier = kBaseCell; cell0.textLabel_text = @"UITableViewCell"; cell0.textLabel_textColor = [UIColor brownColor]; cell0.detailTextLabel_text = @"detailTextLabel_text"; cell0.detailTextLabel_textColor = [UIColor brownColor]; cell0.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell0.selectCellBlock = ^(WynBaseStaticCell *cell, BaseCellDescriber *describer) { NSLog(@"*=*=%s=*=*", __func__); }; return cell0; } inSection:1];
推荐用第二种吧。
阅读全文
1 0
- 如何写静态tableView页面
- 写静态页面
- asp.net生成静态页面时如何写模板
- iOS 代码如何创建静态tableView
- Swift - 使用TableView的静态单元格进行页面布局
- Xcode8 TableView使用静态表单页面显示空白的问题
- .net如何生成静态页面
- 如何动态生成静态页面
- 如何静态化JSP页面
- 如何实现页面静态化:
- C#如何生成静态页面
- 无刷新静态页面怎么写
- asp.net动态页面如何静态页面
- 如何写移动端页面
- 如何写静态代码检查规则(JAVA)
- 动态网站如何生成静态页面
- ASP.NET页面如何建立静态缓存
- Asp.net如何生成html静态页面
- Linux ps查看进程状态及进程死锁问题的查看
- C#中字符数组,字节数组和string之间的转化
- 解决Windows对JDK默认版本切换问题
- 20170828Link
- 直接插入排序
- 如何写静态tableView页面
- 聊聊架构设计
- MongoDB可视化图形界面mongo-express/adminMongo安装过程
- 基于IDEA的aspectj项目demo
- IntelliJ IDEA
- Java注解-三种內建注解
- 【已解决】为什么Python打印数据显示为b‘XXX’格式
- IIC总线、SPI总线、串口232通信、422通信、485通信、TCP/IP协议
- [知了堂学习笔记]_牵线Eclipse和Tomcat第一篇 —— 配置Java环境变量&&安装eclipse