本文共 3240 字,大约阅读时间需要 10 分钟。
上篇博客写了ViewController的基类的实现,这篇博客主要写在BaseViewController的基础上实现一个含tableView控件的基类的实现,主要给包含tableView的页面来继承。
BaseTableViewViewController.h代码:#import "BZBaseViewController.h"#import "BZBaseTableViewCell.h"@interface BZBaseTableViewViewController : BZBaseViewController@property(nonatomic,strong) UITableView * tableView;@property(nonatomic,strong) NSArray * dataSource;-(void)setupTableView;@end
BaseTableViewViewController.m代码:
#import "BZBaseTableViewViewController.h"@interface BZBaseTableViewViewController ()@end@implementation BZBaseTableViewViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.}-(void)setupTableView{ [self.view addSubview:self.tableView];}#pragma mark - UITableViewDelegate & UITableViewDataSource-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 0;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 0.0;}-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.001;}-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.001;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ BZBaseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"]; if (!cell) { cell = [[BZBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"]; } return cell;}#pragma mark - lazy-(UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.mNavigationbarHeight) style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.backgroundColor = [UIColor clearColor]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; } return _tableView;}@end
项目中包含tableView的ViewController都可以继承自该类,重写tableView的代理方法就可以实现相应的功能。
另外,tableView的实现离不开tableViewCell,所以我们也可以写一个BaseTableViewCell,让其他的tableViewCell来继承。 BaseTableViewCell.h代码:#import@interface BZBaseTableViewCell : UITableViewCell-(void)setupUI;@end
BaseTableViewCell.m代码:
#import "BZBaseTableViewCell.h"@implementation BZBaseTableViewCell- (void)awakeFromNib { [super awakeFromNib]; // Initialization code}-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; self.selectionStyle = UITableViewCellSelectionStyleNone; if (self) { [self setupUI]; } return self;}-(void)setupUI{}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end
继承自BaseTableViewCell的子类直接实现-(void)setupUI;方法即可实现自定义的cell界面。
转载地址:http://wcifb.baihongyu.com/