From 214719b7cf023be95225d1132b31d26f61daf4f9 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 10 Dec 2019 17:15:42 +0800 Subject: [PATCH] feat:add onLoadMore for list --- Pod/Classes/Shader/DoricListNode.m | 31 ++++++++++++++++++++++++----- Pod/Classes/Shader/DoricSuperNode.h | 2 ++ Pod/Classes/Shader/DoricSuperNode.m | 7 ++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Pod/Classes/Shader/DoricListNode.m b/Pod/Classes/Shader/DoricListNode.m index 8cb332ce..a5ff7277 100644 --- a/Pod/Classes/Shader/DoricListNode.m +++ b/Pod/Classes/Shader/DoricListNode.m @@ -62,6 +62,10 @@ @interface DoricListNode () @property(nonatomic, strong) NSMutableDictionary *itemHeights; @property(nonatomic, assign) NSUInteger itemCount; @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 @implementation DoricListNode @@ -95,11 +99,22 @@ - (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id self.itemCount = [prop unsignedIntegerValue]; [self.view reloadData]; } else if ([@"renderItem" isEqualToString:name]) { - [self.itemViewIds removeAllObjects]; - [self clearSubModel]; - [self.view reloadData]; + if (![self.renderItemFuncId isEqualToString:prop]) { + self.renderItemFuncId = prop; + [self.itemViewIds.allValues forEach:^(NSString *obj) { + [self removeSubModel:obj]; + }]; + [self.itemViewIds removeAllObjects]; + [self.view reloadData]; + } } else if ([@"batchCount" isEqualToString:name]) { 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 { [super blendView:view forPropName:name propValue:prop]; } @@ -110,7 +125,7 @@ - (void)blend:(NSDictionary *)props { } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return self.itemCount; + return self.itemCount + (self.loadMore ? 1 : 0); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { @@ -118,7 +133,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N NSDictionary *model = [self itemModelAt:position]; NSDictionary *props = model[@"props"]; 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"]; if (!cell) { cell = [[DoricTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId ?: @"doriccell"]; @@ -148,6 +166,9 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa } - (NSDictionary *)itemModelAt:(NSUInteger)position { + if (position >= self.itemCount) { + return [self subModelOf:self.loadMoreViewId]; + } NSString *viewId = self.itemViewIds[@(position)]; if (viewId && viewId.length > 0) { return [self subModelOf:viewId]; diff --git a/Pod/Classes/Shader/DoricSuperNode.h b/Pod/Classes/Shader/DoricSuperNode.h index ec456c99..eef33d3b 100644 --- a/Pod/Classes/Shader/DoricSuperNode.h +++ b/Pod/Classes/Shader/DoricSuperNode.h @@ -35,6 +35,8 @@ - (void)clearSubModel; +- (void)removeSubModel:(NSString *)viewId; + - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId; - (void)recursiveMixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel; diff --git a/Pod/Classes/Shader/DoricSuperNode.m b/Pod/Classes/Shader/DoricSuperNode.m index 232722b4..10803205 100644 --- a/Pod/Classes/Shader/DoricSuperNode.m +++ b/Pod/Classes/Shader/DoricSuperNode.m @@ -149,11 +149,16 @@ - (void)clearSubModel { [self.subNodes removeAllObjects]; } +- (void)removeSubModel:(NSString *)viewId { + [self.subNodes removeObjectForKey:viewId]; +} + - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId { NSAssert(NO, @"Should override class:%@ ,method:%@.", NSStringFromClass([self class]), - NSStringFromSelector(_cmd)); + NSStringFromSelector(_cmd)); return nil; } + - (void)requestLayout { [self.view setNeedsLayout]; }