feat:List add header and footer

This commit is contained in:
pengfei.zhou
2021-10-09 16:50:34 +08:00
committed by osborn
parent f6d117da06
commit 5224be8f90
13 changed files with 243 additions and 46 deletions

View File

@@ -53,6 +53,8 @@ @interface DoricListNode () <UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, copy) NSString *onLoadMoreFuncId;
@property(nonatomic, copy) NSString *renderItemFuncId;
@property(nonatomic, copy) NSString *loadMoreViewId;
@property(nonatomic, copy) NSString *headerViewId;
@property(nonatomic, copy) NSString *footerViewId;
@property(nonatomic, assign) BOOL loadMore;
@property(nonatomic, assign) NSUInteger loadAnchor;
@property(nonatomic, strong) NSMutableSet <DoricDidScrollBlock> *didScrollBlocks;
@@ -93,6 +95,14 @@ - (UITableView *)build {
}];
}
- (BOOL)hasHeader {
return self.headerViewId && self.headerViewId.length > 0;
}
- (BOOL)hasFooter {
return self.footerViewId && self.footerViewId.length > 0;
}
- (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id)prop {
if ([@"scrollable" isEqualToString:name]) {
self.view.scrollEnabled = [prop boolValue];
@@ -126,17 +136,17 @@ - (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id
dispatch_async(dispatch_get_main_queue(), ^{
[view scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[prop unsignedIntegerValue] inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
});
} else if ([@"header" isEqualToString:name]) {
self.headerViewId = prop;
} else if ([@"footer" isEqualToString:name]) {
self.footerViewId = prop;
} else {
[super blendView:view forPropName:name propValue:prop];
}
}
- (void)blend:(NSDictionary *)props {
[super blend:props];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.itemCount + (self.loadMore ? 1 : 0);
return self.itemCount + (self.loadMore ? 1 : 0) + (self.hasHeader ? 1 : 0) + (self.hasFooter ? 1 : 0);
}
- (void)callLoadMore {
@@ -152,13 +162,24 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
NSDictionary *props = model[@"props"];
NSString *reuseId = props[@"identifier"];
self.itemActions[@(position)] = props[@"actions"];
if (position > 0 && position >= self.itemCount && self.onLoadMoreFuncId) {
if (self.hasHeader && position == 0) {
reuseId = @"doricHeaderCell";
} else if (self.hasFooter
&& position == self.itemCount
+ (self.loadMore ? 1 : 0)
+ (self.hasHeader ? 1 : 0)
+ (self.hasFooter ? 1 : 0)) {
reuseId = @"doricFooterCell";
} else if (self.loadMore
&& position > 0
&& position == self.itemCount + (self.hasHeader ? 1 : 0)
&& self.onLoadMoreFuncId) {
reuseId = @"doricLoadMoreCell";
[self callLoadMore];
}
DoricTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId ?: @"doriccell"];
DoricTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId ?: @"doricCell"];
if (!cell) {
cell = [[DoricTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId ?: @"doriccell"];
cell = [[DoricTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId ?: @"doricCell"];
DoricListItemNode *listItemNode = (DoricListItemNode *) [DoricViewNode create:self.doricContext withType:@"ListItem"];
[listItemNode initWithSuperNode:self];
cell.doricListItemNode = listItemNode;
@@ -239,9 +260,22 @@ - (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView tra
}
- (NSDictionary *)itemModelAt:(NSUInteger)position {
if (position >= self.itemCount) {
if (self.hasHeader && position == 0) {
return [self subModelOf:self.headerViewId];
}
if (self.hasFooter && position == self.itemCount
+ (self.loadMore ? 1 : 0)
+ (self.hasHeader ? 1 : 0)
+ (self.hasFooter ? 1 : 0)
- 1) {
return [self subModelOf:self.footerViewId];
}
if (position >= self.itemCount + (self.hasHeader ? 1 : 0)) {
return [self subModelOf:self.loadMoreViewId];
}
if (self.hasHeader) {
position--;
}
NSString *viewId = self.itemViewIds[@(position)];
if (viewId && viewId.length > 0) {
return [self subModelOf:viewId];
@@ -262,7 +296,7 @@ - (NSDictionary *)itemModelAt:(NSUInteger)position {
NSUInteger pos = start + idx;
self.itemViewIds[@(pos)] = thisViewId;
}];
NSString *viewId = self.itemViewIds[@(position)];
viewId = self.itemViewIds[@(position)];
if (viewId && viewId.length > 0) {
return [self subModelOf:viewId];
} else {
@@ -431,18 +465,18 @@ - (void)removeDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
- (void)scrollToItem:(NSDictionary *)params {
BOOL animated = [params[@"animated"] boolValue];
NSUInteger scrolledPosition = [params[@"index"] unsignedIntegerValue];
if (scrolledPosition < self.itemCount && scrolledPosition >= 0) {
for (int i = 0; i <= scrolledPosition; i++) {
[self tableView:self.view cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:scrolledPosition inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:animated];
});
} else {
[self.doricContext.driver.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"scrollToItem Error:%@", @"scrolledPosition range error"]];
message:[NSString stringWithFormat:@"scrollToItem Error:%@", @"scrolledPosition range error"]];
}
}