兩步實(shí)現(xiàn)列表?
1.注冊(cè)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];
進(jìn)階
原理:將UITableViewCell和數(shù)據(jù)整合成一個(gè)CellData恩沽。
@interface QLCellData :NSObject
+ (instancetype)createWithCellClass:(Class)cell data:(NSDictionary*)data;
@property (nonatomic,copy? )NSString * cellId;
@property (nonatomic,strong) NSDictionary *data;
@end
數(shù)據(jù)統(tǒng)一為字典 @{@"item":你傳的值可以為對(duì)象/nsarray/nsstring/nsdictionary等等}裕便。
再將數(shù)據(jù)填充到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的高度通過(guò)實(shí)現(xiàn)protocol來(lái)動(dòng)態(tài)改變做瞪。
@protocol QLBaseCellProtocol
/// 取對(duì)象 動(dòng)態(tài)更新
+ (CGFloat)cellHeight:(NSDictionary*)data;
@end
demo 請(qǐng)移步:
git@github.com:nodrift/BaseCell.git