[iOS]UITableView简单封装

自己写UITableview的时候总是要写很大一串,就写了一个初始化的封装,简化一下代码。

文件放在这里:https://github.com/JakeZhucl/CLBaseObject-C

下面是pod

pod 'CLBaseObject-C'

Swift版本

init(_ delegate : UITableViewDelegate & UITableViewDataSource ,
         _ cells : Array<String>) {
        super.init(frame: CGRect.zero, style: .plain)
        if #available(iOS 11.0, *) {
            self.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        self.estimatedRowHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.delegate = delegate
        self.dataSource = delegate
        
        let CLTableViewNameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        
        for cellName in cells {
            self.register(NSClassFromString(CLTableViewNameSpace+"."+cellName)!, forCellReuseIdentifier: cellName)
        }

        self.tableFooterView = UIView()
        self.separatorStyle = .none
    }

使用方法

//初始化
tableView = CLTableView(self, ["UITableViewCell"])

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell") as! UITableViewCell
        cell.model = model?.indexArray.data[indexPath.row]
        return cell
    }

Object-C版本

- (instancetype)initWithDelegate:(id<UITableViewDelegate,UITableViewDataSource>)delegate
                       cellsName:(NSArray *)cells
{
    self = [super init];
    if (self) {
        if (@available(iOS 11.0, *)) {
            self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        self.estimatedRowHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.delegate = delegate;
        self.dataSource = delegate;
        self.backgroundColor = [UIColor whiteColor];
        for (NSString * cellname in cells) {
            [self registerClass:NSClassFromString(cellname) forCellReuseIdentifier:cellname];
        }
        self.tableFooterView = [UIView new];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

使用方法

//初始化
[[CLTableView alloc]initWithDelegate:self cellsName:@[@"UITableViewCell"]];

//cell代理里面
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    return cell;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今年的春晚,和往年一样的没有看。只是给家人包饺子预备交夜的时候吃的时候,看见姜昆又出来了,一个小品,是他又掉进老虎...
    上善若水sunny阅读 4,042评论 21 13
  • 认识一朋友,因他有些社会背景,毎次聚会不免都有些仰仗和巴结的情绪,尽管知道自己显得不太自然,可想着日后或许有什...
    57c3811d1454阅读 139评论 0 0
  • 机器码和字节码 字节码是一种中间状态(中间码)的二进制代码(文件) 机器码也是二进制代码,是电脑CPU直接读取运行...
    清水芦苇阅读 528评论 0 0
  • 如果时间能够倒流,我必定会如避蛇蝎一样避开他,不加那个qq,我们应该永远像平行线一样,永远都不要有交集。从郁郁...
    米兔2016阅读 84评论 0 0