利用空閑時間封裝了一下tableView集漾,.h和.m文件以及如何調(diào)用均已注釋闪幽,粘貼過去就可以用了击敌,簡單粗暴费封。
//
// CommonTableView.h
// TRY
//
// Created by Jianwei Dong on 2018/6/29.
// Copyright ? 2018年 Jianwei Dong. All rights reserved.
//
import <UIKit/UIKit.h>
/點擊cell觸發(fā)此回調(diào)方法/
typedef void(^SelectCell)(NSIndexPath indexPath);
/在此block中返回你要創(chuàng)建的cell*/
typedef UITableViewCell *(^CreateCell)(NSIndexPath *indexPath);
/CommonTableView繼承UITableView,遵守tableView協(xié)議/
@interface CommonTableView : UITableView<UITableViewDelegate,UITableViewDataSource>
/重構(gòu)tableiView初始化方法,在需要的地方調(diào)用此初始化方法傳入相應參數(shù)即可/
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style rowCount:(NSInteger)rows cellHeight:(CGFloat)height cell:(CreateCell)cell selectedCell:(SelectCell)selectBlock;
/*
-
(void)viewDidLoad {
[super viewDidLoad];
//刷新數(shù)據(jù)
[self.tableView reloadData];
// Do any additional setup after loading the view.
}
//懶加載tableView
-(CommonTableView *)tableView
{
if (!_tableView) {__weak UITableView * tb = _tableView; _tableView = [[CommonTableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain rowCount:[self.dataList count] cellHeight:100 cell:^UITableViewCell *(NSIndexPath *indexPath) { // 創(chuàng)建你自定義的cell static NSString *identifier = @"cell"; TryTableViewCell *cell = [tb dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[TryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = self.dataList[indexPath.row]; return cell; } selectedCell:^(NSIndexPath *indexPath) { //點擊cell執(zhí)行此方法 NSLog(@"網(wǎng)%ld",indexPath.row); }]; [self.view addSubview:_tableView];
}
return _tableView;
}
*/
@end
/***********************以下為.m文件****************************/
//
// CommonTableView.m
// TRY
//
// Created by Jianwei Dong on 2018/6/29.
// Copyright ? 2018年 Jianwei Dong. All rights reserved.
//
import "CommonTableView.h"
@interface CommonTableView ()
/rows/
@property (nonatomic)NSInteger rows;
/cell的高度/
@property(nonatomic,assign)CGFloat height;
/聲明自定義類型變量/
@property (nonatomic,copy)SelectCell selectBlock;
/聲明自定義類型變量/
@property(nonatomic,copy)CreateCell createCell;
@end
@implementation CommonTableView
/初始化方法的實現(xiàn)/
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style rowCount:(NSInteger)rows cellHeight:(CGFloat)height cell:(CreateCell)cell selectedCell:(SelectCell)selectBlock
{
self = [super initWithFrame:frame style:style];
if (self) {
self.dataSource = self;
self.delegate = self;
self.rows = rows;
self.height = height;
self.createCell = cell;
self.selectBlock = selectBlock;
self.tableFooterView = [[UIView alloc]init];
}
return self;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = self.createCell(indexPath);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.selectBlock(indexPath);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.rows;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.height;
}
@end