feat:add onLoadMore for list

This commit is contained in:
pengfei.zhou 2019-12-10 17:15:42 +08:00
parent f8a420b427
commit 214719b7cf
3 changed files with 34 additions and 6 deletions

View File

@ -62,6 +62,10 @@ @interface DoricListNode () <UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSNumber *> *itemHeights; @property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSNumber *> *itemHeights;
@property(nonatomic, assign) NSUInteger itemCount; @property(nonatomic, assign) NSUInteger itemCount;
@property(nonatomic, assign) NSUInteger batchCount; @property(nonatomic, assign) NSUInteger batchCount;
@property(nonatomic, copy) NSString *onLoadMoreFuncId;
@property(nonatomic, copy) NSString *renderItemFuncId;
@property(nonatomic, copy) NSString *loadMoreViewId;
@property(nonatomic, assign) BOOL loadMore;
@end @end
@implementation DoricListNode @implementation DoricListNode
@ -95,11 +99,22 @@ - (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id
self.itemCount = [prop unsignedIntegerValue]; self.itemCount = [prop unsignedIntegerValue];
[self.view reloadData]; [self.view reloadData];
} else if ([@"renderItem" isEqualToString:name]) { } else if ([@"renderItem" isEqualToString:name]) {
if (![self.renderItemFuncId isEqualToString:prop]) {
self.renderItemFuncId = prop;
[self.itemViewIds.allValues forEach:^(NSString *obj) {
[self removeSubModel:obj];
}];
[self.itemViewIds removeAllObjects]; [self.itemViewIds removeAllObjects];
[self clearSubModel];
[self.view reloadData]; [self.view reloadData];
}
} else if ([@"batchCount" isEqualToString:name]) { } else if ([@"batchCount" isEqualToString:name]) {
self.batchCount = [prop unsignedIntegerValue]; self.batchCount = [prop unsignedIntegerValue];
} else if ([@"onLoadMore" isEqualToString:name]) {
self.onLoadMoreFuncId = prop;
} else if ([@"loadMoreView" isEqualToString:name]) {
self.loadMoreViewId = prop;
} else if ([@"loadMore" isEqualToString:name]) {
self.loadMore = [prop boolValue];
} else { } else {
[super blendView:view forPropName:name propValue:prop]; [super blendView:view forPropName:name propValue:prop];
} }
@ -110,7 +125,7 @@ - (void)blend:(NSDictionary *)props {
} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.itemCount; return self.itemCount + (self.loadMore ? 1 : 0);
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@ -118,7 +133,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
NSDictionary *model = [self itemModelAt:position]; NSDictionary *model = [self itemModelAt:position];
NSDictionary *props = model[@"props"]; NSDictionary *props = model[@"props"];
NSString *reuseId = props[@"identifier"]; NSString *reuseId = props[@"identifier"];
if (position > 0 && position >= self.itemCount && self.onLoadMoreFuncId) {
reuseId = @"doricLoadMoreCell";
[self callJSResponse:self.onLoadMoreFuncId, nil];
}
DoricTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId ?: @"doriccell"]; DoricTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId ?: @"doriccell"];
if (!cell) { if (!cell) {
cell = [[DoricTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId ?: @"doriccell"]; cell = [[DoricTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId ?: @"doriccell"];
@ -148,6 +166,9 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
} }
- (NSDictionary *)itemModelAt:(NSUInteger)position { - (NSDictionary *)itemModelAt:(NSUInteger)position {
if (position >= self.itemCount) {
return [self subModelOf:self.loadMoreViewId];
}
NSString *viewId = self.itemViewIds[@(position)]; NSString *viewId = self.itemViewIds[@(position)];
if (viewId && viewId.length > 0) { if (viewId && viewId.length > 0) {
return [self subModelOf:viewId]; return [self subModelOf:viewId];

View File

@ -35,6 +35,8 @@
- (void)clearSubModel; - (void)clearSubModel;
- (void)removeSubModel:(NSString *)viewId;
- (DoricViewNode *)subNodeWithViewId:(NSString *)viewId; - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId;
- (void)recursiveMixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel; - (void)recursiveMixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel;

View File

@ -149,11 +149,16 @@ - (void)clearSubModel {
[self.subNodes removeAllObjects]; [self.subNodes removeAllObjects];
} }
- (void)removeSubModel:(NSString *)viewId {
[self.subNodes removeObjectForKey:viewId];
}
- (DoricViewNode *)subNodeWithViewId:(NSString *)viewId { - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
NSAssert(NO, @"Should override class:%@ ,method:%@.", NSStringFromClass([self class]), NSAssert(NO, @"Should override class:%@ ,method:%@.", NSStringFromClass([self class]),
NSStringFromSelector(_cmd)); NSStringFromSelector(_cmd));
return nil; return nil;
} }
- (void)requestLayout { - (void)requestLayout {
[self.view setNeedsLayout]; [self.view setNeedsLayout];
} }