iOS UITableView的封裝

利用空閑時間封裝了一下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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末焕妙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子弓摘,更是在濱河造成了極大的恐慌焚鹊,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件韧献,死亡現(xiàn)場離奇詭異末患,居然都是意外死亡,警方通過查閱死者的電腦和手機锤窑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門璧针,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人果复,你說我怎么就攤上這事陈莽。” “怎么了虽抄?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵走搁,是天一觀的道長。 經(jīng)常有香客問我迈窟,道長私植,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任车酣,我火速辦了婚禮曲稼,結(jié)果婚禮上索绪,老公的妹妹穿的比我還像新娘。我一直安慰自己贫悄,他們只是感情好瑞驱,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著窄坦,像睡著了一般唤反。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鸭津,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天彤侍,我揣著相機與錄音,去河邊找鬼逆趋。 笑死盏阶,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的闻书。 我是一名探鬼主播名斟,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼惠窄!你這毒婦竟也來了蒸眠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤杆融,失蹤者是張志新(化名)和其女友劉穎楞卡,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脾歇,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡蒋腮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了藕各。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片池摧。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖激况,靈堂內(nèi)的尸體忽然破棺而出作彤,到底是詐尸還是另有隱情,我是刑警寧澤乌逐,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布竭讳,位于F島的核電站,受9級特大地震影響浙踢,放射性物質(zhì)發(fā)生泄漏绢慢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一洛波、第九天 我趴在偏房一處隱蔽的房頂上張望胰舆。 院中可真熱鬧骚露,春花似錦、人聲如沸缚窿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽滨攻。三九已至够话,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間光绕,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工畜份, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留诞帐,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓爆雹,卻偏偏與公主長得像停蕉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子钙态,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容