ZXTableView(iOS開發【快速、高效地構建TableView】)

ZXTableView(快速、高效地構建TableView)

github地址

安裝

通過CocoaPods安裝

pod 'ZXTableView'

手動導入

  • 將ZXTableView拖入項目中。

導入頭文件

#import "ZXTableView.h"

創建ZXTableView示例

創建一個最基礎的TableView,實現點擊刪除按鈕刪除對應行

  • 在TableView所在的控制器中,此處定義的cell對應模型為ZXTestSingleTbModel
//聲明cell是什么類
self.tableView.zx_setCellClassAtIndexPath = ^Class (NSIndexPath *  indexPath) {
    return [ZXTestSingleTbCell class];
};
//獲取cell對象并對其進行處理
__weak __typeof(self) weakSelf = self;
self.tableView.zx_getCellAtIndexPath = ^(NSIndexPath *indexPath, ZXTestSingleTbCell *cell, id model) {
    cell.delBlock = ^{
        [weakSelf.tableView.zxDatas removeObjectAtIndex:indexPath.row];
        [weakSelf.tableView reloadData];
    };
};
//設置ZXTableView的數據,dataArr即為ZXTestSingleTbModel模型數組,如果需要多個section的效果,只需要改變dataArr即可。
self.tableView.zxDatas = dataArr;
  • 在ZXTestSingleTbCell中
#import "ZXTestSingleTbCell.h"
#import "ZXTestSingleTbModel.h"
@interface ZXTestSingleTbCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImgV;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *goodAtLabel;
@property (weak, nonatomic) IBOutlet UIButton *delBtn;
//若cell中有包含model的屬性,則會自動將model賦值給它(如果有多個含有model字符串的屬性,則賦值給第一個)
@property (strong,nonatomic) ZXTestSingleTbModel *sTbModel;
@end

//重寫model的set方法即可
-(void)setSTbModel:(ZXTestSingleTbModel *)sTbModel{
    _sTbModel = sTbModel;
    self.iconImgV.image = sTbModel.iconImg;
    self.nameLabel.text = sTbModel.name;
    self.goodAtLabel.text = sTbModel.goodAt;
}
  • 查看效果
    <img src="http://www.zxlee.cn/ZXTableViewDemoImg/singleDemo.png"/>

創建一個含有HeaderView與FooterView的TableView

  • 在TableView所在的控制器中,此處定義的cell對應模型為ZXTestSingleTbModel
//聲明cell是什么類
self.tableView.zx_setCellClassAtIndexPath = ^Class (NSIndexPath *  indexPath) {
    return [ZXTestSingleTbCell class];
};
//聲明HeaderView是什么類
self.tableView.zx_setHeaderClassInSection = ^Class(NSInteger section) {
    return [ZXTestHFHeaderView class];
};
//聲明FooterView是什么類
self.tableView.zx_setFooterClassInSection = ^Class(NSInteger section) {
    return [ZXTestHFFooterView class];
};
//獲取HeaderView對象并對其進行處理
self.tableView.zx_getHeaderViewInSection = ^(NSUInteger section, ZXTestHFHeaderView *headerView, NSMutableArray *secArr) {
    headerView.headerLabel.text = [NSString stringWithFormat:@"HeaderView--%lu",section];
};
//獲取FooterView對象并對其進行處理
self.tableView.zx_getFooterViewInSection = ^(NSUInteger section, ZXTestHFFooterView *footerView, NSMutableArray *secArr) {
    footerView.footerLabel.text = [NSString stringWithFormat:@"FooterView--%lu",section];
};
//設置ZXTableView的數據,dataArr即為ZXTestSingleTbModel模型數組,dataArr中包含多個數組。
self.tableView.zxDatas = dataArr;
  • 在ZXTestSingleTbCell中的處理同上
  • 查看效果
    <img src="http://www.zxlee.cn/ZXTableViewDemoImg/secDemo.png"/>

創建動態高度的TableView

  • 在TableView所在的控制器中,此處定義的cell對應模型為ZXTestCHTbModel
#pragma mark 設置TableView
//聲明cell是什么類
self.tableView.zx_setCellClassAtIndexPath = ^Class (NSIndexPath *  indexPath) {
    return [ZXTestCHTbCell class];
};
//聲明HeaderView是什么類
self.tableView.zx_setHeaderClassInSection = ^Class(NSInteger section) {
    return [ZXTestCHTbSpaceHeader class];
};
//設置ZXTableView的數據,dataArr即為ZXTestCHTbModel模型數組
self.tableView.zxDatas = dataArr;
  • ZXTestCHTbCelll中的處理同上

  • 在ZXTestCHTbModel.h中

@interface ZXTestCHTbModel : NSObject
@property (strong,nonatomic) UIImage *iconImg;
@property (copy,nonatomic) NSString *name;
@property (copy,nonatomic) NSString *time;
@property (copy,nonatomic) NSString *comment;
//此處聲明了cellH,則ZXTableView會自動把cell高度賦值給cellH,更改cellH即可改變cell高度
@property (assign,nonatomic) CGFloat cellH;
@end
  • 在ZXTestCHTbModel.m中
#import "ZXTestCHTbModel.h"

@implementation ZXTestCHTbModel
-(void)setComment:(NSString *)comment{
    _comment = comment;
    //此處comment所顯示對應的Label距離左右邊距離為15,字體大小為14,cell頂部顯示個人信息的View高度為50,commentLabel距離上下均為10
    CGFloat commentH = [self getStrHeightWithText:comment font:[UIFont systemFontOfSize:14] viewWidth:[UIScreen mainScreen].bounds.size.width - 15 * 2];
    //將計算的cell高度賦值給cellH即可
    self.cellH = commentH + 10 * 2 + 50;
    
}
//獲取文字高度
- (CGFloat)getStrHeightWithText:(NSString *)text font:(UIFont *)font viewWidth:(CGFloat)width {
    NSDictionary *attrs = @{NSFontAttributeName :font};
    CGSize maxSize = CGSizeMake(width, MAXFLOAT);
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
    return  ceilf(size.height);
}
@end
  • 查看效果
    <img src="http://www.zxlee.cn/ZXTableViewDemoImg/dynamicHDemo.png"/>

具體使用說明

如何快速使用

  • 創建一個tableView的步驟大致分為,聲明cell,聲明headerView&footerView,self.tableView.zxDatas賦值,在cell中聲明一個含有“model”的屬性名,重寫該屬性的set方法即可。
  • ZXTableView中的大多數方法都是zx_開頭,zx_set開頭代表設置tableView,例如:zx_setCellClass...即為設置(聲明)cell的類是誰;zx_get開頭代表從tableView中獲取信息,例如zx_getCellAt...即為獲取cell對象,可依據此結合下方說明快速記憶。

cell相關

聲明cell

self.tableView.zx_setCellClassAtIndexPath = ^Class (NSIndexPath *  indexPath) {
    //可以根據indexPath返回不同的cell
    return [MyCell class];
};

獲取cell對象,對cell對象進行操作

self.tableView.zx_getCellAtIndexPath = ^(NSIndexPath *indexPath, id cell, id model) {
    //這里的id cell中id可以改成自己當前的cell類名(若只有一種cell),id model中的id可以改成自己當前模型的類名(若只有一種模型)
}

以上聲明cell類與獲取cell可以寫在同一個方法中

[self.tableView zx_setCellClassAtIndexPath:^Class(NSIndexPath *indexPath) {
    return [MyCell class];
} returnCell:^(NSIndexPath *indexPath, id cell, id model) {
    //獲取cell對象
}];

(非必須)設置cell高度

//返回cell高度,ZXTableView默認會將cell高度設置為cell本身高度,也就是xib中cell的高度或純代碼中在初始化方法中設置的cell的高度,若需要改動,則可以使用以下方法實現。
self.tableView.zx_setCellHAtIndexPath = ^CGFloat(NSIndexPath *indexPath) {
    return 70;
};

滑動編輯

self.tableView.zx_editActionsForRowAtIndexPath = ^NSArray<UITableViewRowAction *> *(NSIndexPath *indexPath) {
        UITableViewRowAction *delAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            [weakSelf.tableView.zxDatas removeObjectAtIndex:indexPath.row];
            [weakSelf.tableView reloadData];
        }];
        //第0行不顯示側滑刪除,其余行顯示側滑刪除,這里只是為了演示控制側滑刪除行的情況
        if(indexPath.row == 0){
            return nil;
        }
        return @[delAction];
    };

設置cell高度的幾種方式

  • 在控制器中設置cell高度,可根據indexPath設置不同的cell高度(優先級最高,若設置了,其他高度設置方法均無效)
self.tableView.zx_setCellHAtIndexPath = ^CGFloat(NSIndexPath *indexPath) {
    return 70;
};
  • 在cell中設置cell的高度(優先級最低)
//在cell的初始化方法中設置cell高度即可
self.height = 50;
  • 在model中設置cell的高度(優先級第二,若設置,則在cell中設置cell的高度無效)

在model.h中

//此處聲明了cellH,則ZXTableView會自動把cell高度賦值給cellH,更改cellH即可改變cell高度
@property (assign,nonatomic) CGFloat cellH;

在model.m中,在需要的時候更改cellH

//例如,可以重寫cellH的set方法,將cell高度在原先基礎上增加10
-(void)setCellH:(CGFloat)cellH{
    _cellH = cellH + 10;
}

在cell或model中獲取當前的indexPath

  • 在cell中獲取當前的indexPath
//在Cell.h或Cell.m中定義屬性indexPath即可
@property (strong, nonatomic) NSIndexPath *indexPath;
  • 在model中獲取當前的indexPath
//在Model.h或Model.m中定義屬性indexPath即可
@property (strong, nonatomic) NSIndexPath *indexPath;

headerView&footerView相關,此處以headerView為例

聲明headerView

//聲明HeaderView是什么類
self.tableView.zx_setHeaderClassInSection = ^Class(NSInteger section) {
    return [MyHeaderView class];
};

獲取headerView對象

//獲取HeaderView對象并對其進行處理
self.tableView.zx_getHeaderViewInSection = ^(NSUInteger section, MyHeaderView *headerView, NSMutableArray *secArr) {
    headerView.headerLabel.text = [NSString stringWithFormat:@"HeaderView--%lu",section];
};

以上聲明headerView類與獲取headerView可以寫在同一個方法中

[self.tableView zx_setHeaderClassInSection:^Class(NSInteger) {
    return [MyHeaderView cell];
} returnHeader:^(NSUInteger section, id headerView, NSMutableArray *secArr) {
  //獲取headerView對象
}];

(非必須)設置headerView高度

//返回headerViewl高度,ZXTableView默認會將headerView高度設置為headerView本身高度,也就是xib中headerView的高度或純代碼中在初始化方法中設置的headerView的高度,若需要改動,則可以使用以下方法實現。
self.tableView.zx_setHeaderHInSection = ^CGFloat(NSInteger section) {
    return 100;
};

設置headerView&footerView高度的幾種方式

  • 在控制器中設置headerView高度
self.tableView.zx_setHeaderHInSection = ^CGFloat(NSInteger section) {
    return 100;
};
  • 在headerView中設置headerView高度
//在headerView的初始化方法中設置headerView高度即可
self.height = 100;

在headerView或footerView中獲取當前的section

  • 在headerView中獲取當前的section
//在headerView.h或headerView.m中定義屬性section即可
@property (strong, nonatomic) NSNumber *section;

headerView&footerView屬性相關,此處以headerView為例

  • 無數據是否顯示HeaderView,默認為YES
//無數據時不顯示headerView
self.tableView.zx_showHeaderWhenNoMsg = NO;
  • 保持headerView不變(僅初始化一次),默認為NO
//保持headerView只初始化一次,之后不再重新創建
self.tableView.zx_keepStaticHeaderView = YES;

tableView代理事件&偏好設置相關

  • 點擊了某一行cell
//點擊了某一行cell
self.tableView.zx_didSelectedAtIndexPath = ^(NSIndexPath *indexPath, id model, id cell) {
  //這里的id cell中id可以改成自己當前的cell類名(若只有一種cell),id model中的id可以改成自己當前模型的類名(若只有一種模型)
  
};
  • 取消點擊某一行cell
//取消點擊某一行cell
self.tableView.zx_didDeSelectedAtIndexPath = ^(NSIndexPath *indexPath, id model, id cell) {
  //這里的id cell中id可以改成自己當前的cell類名(若只有一種cell),id model中的id可以改成自己當前模型的類名(若只有一種模型)
  
};
  • 禁止系統Cell自動高度 可以有效解決tableView跳動問題,默認為YES
self.tableView.zx_disableAutomaticDimension = YES;
  • 無數據是否顯示HeaderView,默認為YES
self.tableView.zx_showHeaderWhenNoMsg = YES;
  • 無數據是否顯示FooterView,默認為YES
self.tableView.zx_showFooterWhenNoMsg = YES;
  • 控制獲取cell回調在獲取model之后,默認為NO
self.tableView.zx_fixCellBlockAfterAutoSetModel = YES;
  • scrollView相關代理
///scrollView滾動事件
@property (nonatomic, copy) void (^zx_scrollViewDidScroll)(UIScrollView *scrollView);
///scrollView縮放事件
@property (nonatomic, copy) void (^zx_scrollViewDidZoom)(UIScrollView *scrollView);
///scrollView滾動到頂部事件
@property (nonatomic, copy) void (^zx_scrollViewDidScrollToTop)(UIScrollView *scrollView);
///scrollView開始拖拽事件
@property (nonatomic, copy) void (^zx_scrollViewWillBeginDragging)(UIScrollView *scrollView);
///scrollView開始拖拽事件
@property (nonatomic, copy) void (^zx_scrollViewDidEndDragging)(UIScrollView *scrollView, BOOL willDecelerate);
  • tableView重寫數據源與代理
//tableView的DataSource 設置為當前控制器即可重寫對應數據源方法
@property (nonatomic, weak, nullable) id <UITableViewDataSource> zxDataSource;
//tableView的Delegate 設置為當前控制器即可重寫對應代理方法
@property (nonatomic, weak, nullable) id <UITableViewDelegate> zxDelegate;

ZXTableViewConfig(ZXTableView配置文件)

///model默認去匹配的cell高度屬性名 若不存在則動態生成cellHRunTime的屬性名
static NSString *const CELLH = @"cellH";
///cell會自動賦值包含“model”的屬性
static NSString *const DATAMODEL = @"model";
///model與cell的index屬性,存儲當前model與cell所屬的indexPath
static NSString *const INDEX = @"indexPath";
///headerView與footerView的section屬性,存儲當前headerView與footerView所屬的section
static NSString *const SECTION = @"section";
///若ZXBaseTableView無法自動獲取cell高度(zxdata有值即可),且用戶未自定義高度,則使用默認高度
static CGFloat const CELLDEFAULTH = 44;

#pragma mark - TableView默認偏好配置
///無數據是否顯示HeaderView,默認為YES
static BOOL const ShowHeaderWhenNoMsg = YES;
///無數據是否顯示FooterView,默認為YES
static BOOL const ShowFooterWhenNoMsg = YES;
///保持headerView不變(僅初始化一次),默認為NO
static BOOL const KeepStaticHeaderView = NO;
///保持footerView不變(僅初始化一次),默認為NO
static BOOL const KeepStaticFooterView = NO;
///禁止系統Cell自動高度 可以有效解決tableView跳動問題,默認為YES
static BOOL const DisableAutomaticDimension = YES;
///分割線樣式,默認為UITableViewCellSeparatorStyleNone
static BOOL const DefaultSeparatorStyle =  UITableViewCellSeparatorStyleNone;

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容