iOS UITableView的封裝

利用空閑時(shí)間封裝了一下tableView,.h和.m文件以及如何調(diào)用均已注釋,粘貼過(guò)去就可以用了,簡(jiǎn)單粗暴。
//
// CommonTableView.h
// TRY
//
// Created by Jianwei Dong on 2018/6/29.
// Copyright ? 2018年 Jianwei Dong. All rights reserved.
//

import <UIKit/UIKit.h>

/點(diǎn)擊cell觸發(fā)此回調(diào)方法/
typedef void(^SelectCell)(NSIndexPath indexPath);
/
在此block中返回你要?jiǎng)?chuàng)建的cell*/
typedef UITableViewCell *(^CreateCell)(NSIndexPath *indexPath);

/CommonTableView繼承UITableView,遵守tableView協(xié)議/
@interface CommonTableView : UITableView<UITableViewDelegate,UITableViewDataSource>

/重構(gòu)tableiView初始化方法,在需要的地方調(diào)用此初始化方法傳入相應(yīng)參數(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) {
    
         //點(diǎn)擊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

/初始化方法的實(shí)現(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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說(shuō),表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無(wú)邪8閱讀 10,962評(píng)論 3 3
  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,300評(píng)論 3 38
  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風(fēng)格:UITa...
    蕭雪痕閱讀 2,992評(píng)論 2 10
  • 前言 最近忙完項(xiàng)目比較閑,想寫一篇博客來(lái)分享一些自學(xué)iOS的心得體會(huì),希望對(duì)迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,588評(píng)論 9 5
  • 1. 魚(yú)骨圖解決法,對(duì)可能因素進(jìn)行羅舉分類,不容易遺漏。與triz理論里的矩陣表對(duì)應(yīng)起來(lái)。 2.盡信書不如無(wú)書,具...
    觀明6閱讀 691評(píng)論 0 0

友情鏈接更多精彩內(nèi)容