iOS: Optimize the call of [tableview scrollToRowAtIndexPath] method

This commit is contained in:
Jiang Teng 2023-01-16 19:08:31 +08:00 committed by osborn
parent 7ae96b2cea
commit c7d7e4052d

View File

@ -280,6 +280,21 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
} }
} }
- (void)calculateCellHeightItemNode:(DoricListItemNode *)node atIndexPath:(NSIndexPath *)indexPath {
NSUInteger position = (NSUInteger) indexPath.row;
if (self.itemHeights[@(position)]) {
return;
}
NSDictionary *model = [self itemModelAt:position];
NSDictionary *props = model[@"props"];
[node blend:props];
CGFloat height = node.view.doricLayout.heightSpec == DoricLayoutFit ? CGFLOAT_MAX : self.view.height;
[node.view.doricLayout apply:CGSizeMake(self.view.width, height)];
[node requestLayout];
self.itemHeights[@(position)] = @(node.view.height);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger position = (NSUInteger) indexPath.row; NSUInteger position = (NSUInteger) indexPath.row;
NSNumber *heightNumber = self.itemHeights[@(position)]; NSNumber *heightNumber = self.itemHeights[@(position)];
@ -290,6 +305,17 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
} }
} }
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger position = (NSUInteger) indexPath.row;
if (position < self.itemHeights.count) {
NSNumber *heightNumber = self.itemHeights[@(position)];
if (heightNumber) {
return [heightNumber floatValue];
}
}
return 44.f;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *actions = self.itemActions[@(indexPath.row)]; NSArray *actions = self.itemActions[@(indexPath.row)];
return actions.count > 0; return actions.count > 0;
@ -588,11 +614,13 @@ - (void)scrollToItem:(NSDictionary *)params {
NSUInteger scrolledPosition = [params[@"index"] unsignedIntegerValue]; NSUInteger scrolledPosition = [params[@"index"] unsignedIntegerValue];
if (scrolledPosition < self.rowCount && scrolledPosition >= 0) { if (scrolledPosition < self.rowCount && 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(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
DoricListItemNode *node = (DoricListItemNode *) [DoricViewNode create:self.doricContext withType:@"ListItem"];
[node initWithSuperNode:self];
for (int i = 0; i <= scrolledPosition; i++) {
[self calculateCellHeightItemNode:node atIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.view scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:scrolledPosition inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:animated]; [self.view scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:scrolledPosition inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:animated];
}); });
} else { } else {