iOS记录

优雅的创建 UITableViewCell

2020-09-07  本文已影响0人  bianruifeng


#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

/// 快速创建列表元素
@interface UITableView (cell)

/// 通过Xib、Nib 注册的 Cell
/// @param cellClass 类名
- (void)registerNib:(nullable Class)cellClass;

/// 通过class 注册的 Cell
/// @param cellClass 类名
- (void)registerClass:(nullable Class)cellClass;

/// 根据Xib、Nib注册的 HeaderFooter
/// @param cellClass 类名:需和Xib、Nib同名
- (void)registerHeaderFooterNib:(nullable Class)cellClass;

/// 根据class 注册的 HeaderFooter
/// @param cellClass 类名
- (void)registerHeaderFooterClass:(nullable Class)cellClass;

/// 根据class 获取 cell,未注册返回nil
/// @param cellClass 类名
-(__kindof UITableViewCell *)dequeueReusableCell:(nullable Class)cellClass;

/// 根据class 获取 HeaderFooter,未注册返回nil
/// @param cellClass 类名
-(__kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterView:(nullable Class)cellClass;

/// 根据class名 注册并返回 Cell
/// @param cellClass 类名
-(__kindof UITableViewCell *)cellClass:(nullable Class)cellClass;
/// 根据Xib、Nib 名 注册并返回 Cell
/// @param cellClass 类名
-(__kindof UITableViewCell *)cellNib:(nullable Class)cellClass;

/// 根据class名 注册并返回 HeaderFooter
/// @param cellClass 类名
-(__kindof UITableViewHeaderFooterView *)headerFooterViewWithClass:(nullable Class)cellClass;

/// 根据Xib、Nib 名 注册并返回 HeaderFooter
/// @param cellClass 类名:需和Xib、Nib同名
-(__kindof UITableViewHeaderFooterView *)headerFooterViewWithNib:(nullable Class)cellClass;

@end

NS_ASSUME_NONNULL_END

#import "UITableView+Cell.h"

@implementation UITableView (cell)

- (void)registerNib:(nullable Class)cellClass
{
    NSString *name= NSStringFromClass(cellClass);
    UINib *nib=[UINib nibWithNibName:name bundle:nil];
    if(nib==nil){
        NSLog(@"CellView:从xib创建视图失败,当前类是:%@",name);
    }
    [self registerNib:nib forCellReuseIdentifier:name];
}

- (void)registerClass:(nullable Class)cellClass
{
    NSString *name= NSStringFromClass(cellClass);
    [self registerClass:cellClass forCellReuseIdentifier:name];
}

- (void)registerHeaderFooterNib:(nullable Class)cellClass
{
    NSString *name= NSStringFromClass(cellClass);
    UINib *nib=[UINib nibWithNibName:name bundle:nil];
    if(nib==nil){
        NSLog(@"HeaderFooterView:从xib创建视图失败,当前类是:%@",name);
    }
    [self registerNib:nib forHeaderFooterViewReuseIdentifier:name];
}

- (void)registerHeaderFooterClass:(nullable Class)cellClass
{
    NSString *name= NSStringFromClass(cellClass);
    [self registerClass:cellClass forHeaderFooterViewReuseIdentifier:name];
}

-(__kindof UITableViewCell *)dequeueReusableCell:(nullable Class)cellClass
{
    NSString *name= NSStringFromClass(cellClass);
    return [self dequeueReusableCellWithIdentifier:name];
}

-(__kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterView:(nullable Class)cellClass
{
    NSString *name= NSStringFromClass(cellClass);
    return [self dequeueReusableHeaderFooterViewWithIdentifier:name];
}

-(__kindof UITableViewCell *)cellClass:(nullable Class)cellClass
{
    UITableViewCell * cell = [self dequeueReusableCell:cellClass];
    if (cell) {
        return cell;
    }
    [self registerClass:cellClass];
    return [self dequeueReusableCell:cellClass];
}

-(__kindof UITableViewCell *)cellNib:(nullable Class)cellClass
{
    UITableViewCell * cell = [self dequeueReusableCell:cellClass];
    if (cell) {
        return cell;
    }
    [self registerNib:cellClass];
    return [self dequeueReusableCell:cellClass];
}

-(__kindof UITableViewHeaderFooterView *)headerFooterViewWithClass:(nullable Class)cellClass
{
    UITableViewHeaderFooterView *HeaderFooterView = [self dequeueReusableHeaderFooterView:cellClass];
    if (HeaderFooterView) {
        return HeaderFooterView;
    }
    [self registerHeaderFooterClass:cellClass];
    return [self dequeueReusableHeaderFooterView:cellClass];
}

-(__kindof UITableViewHeaderFooterView *)headerFooterViewWithNib:(nullable Class)cellClass
{
    UITableViewHeaderFooterView *HeaderFooterView = [self dequeueReusableHeaderFooterView:cellClass];
    if (HeaderFooterView) {
        return HeaderFooterView;
    }
    [self registerHeaderFooterNib:cellClass];
    return [self dequeueReusableHeaderFooterView:cellClass];
}
//        static NSString *CellIdentifier = @"UITableViewCell";
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
//        if (!cell) {
//            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//        }
@end


使用:

UITableViewCell* cell = [tableView cellClass:[UITableViewCell class]];
上一篇下一篇

猜你喜欢

热点阅读