iOS-如何只用两三行代码实现列表页面

两步实现列表 

1.注册cell

#define RegistClass(view,class) [view registerClass:class forCellReuseIdentifier:(NSStringFromClass(class))];

RegistClass(self.tableView, [Cell class]);

2.添加cell的Datasource

    [self.cellData addObject:[QLCellData createWithCellClass:[Cell class] data:@{}]];

    [tableView reload];

进阶

原理:将UITableViewCell和数据整合成一个CellData。

@interface QLCellData :NSObject

+ (instancetype)createWithCellClass:(Class)cell data:(NSDictionary*)data;

@property (nonatomic,copy  )NSString * cellId;

@property (nonatomic,strong) NSDictionary *data;

@end

数据统一为字典 @{@"item":你传的值可以为对象/nsarray/nsstring/nsdictionary等等}。

再将数据填充到UITableViewCell中

- (void)fillCell:(UITableViewCell*)cell data:(QLCellData*)rowObj {

    for(NSString* keyPath in rowObj.data.allKeys) {

        if(keyPath.length==0){continue;}

        [cell setValue:rowObj.data[keyPath] forKeyPath:keyPath];

    }

}

cell的高度通过实现protocol来动态改变。

@protocol QLBaseCellProtocol

/// 取对象 动态更新

+ (CGFloat)cellHeight:(NSDictionary*)data;

@end


demo 请移步:

git@github.com:nodrift/BaseCell.git

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容