上下文: 我正在使用 AsyncDisplayKit 加载包含文本和图像的表格。图像是从 Internet 下载的,图像大小在图像下载完成之前是未知的。
我正在尝试做 的事情:图像下载完成后,我想重新调整单元格的大小和布局以适应图像,然后重新调整表格的大小和布局以适应新的单元格高度。
到目前为止我所做的: 我最初仅使用文本绘制 ASTableView,并使用 ASNetworkingImage 委托方法 imageNode:didLoadImage 方法在图像下载完成时获得通知。
- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image {
// Scale image downloaded from Internet to fit constrained width
// Set networkImageNode to scaled image
UIImage *newimage = [self scaleImage:image toConstrainedWidth:CONSTRAINED_WIDTH];
self.networkImageNode.image = newimage;
// Gets the parent cell
// Recalculate cell size and layout the cell (This works!)
MyAsyncTableCellNode *parentCell = ASDisplayNodeFindClass(self, [MyAsyncTableCellNode class]);
[parentCell invalidateCalculatedSize];
[parentCell measure:CGSizeMake(CONSTRAINED_WIDTH, MAXFLOAT)];
[parentCell layout];
// Attempting to re-layout the table to shift the table cells
// to accommodate for the new cell height (ALL these do NOT work!)
MyAsyncTableViewController *parentTableViewController = ASDisplayNodeFindClass(self, [MyAsyncTableViewController class]);
// Attempt #1
[parentTableViewController.asyncTableView reloadData];
// Attempt #2
[parentTableViewController.asyncTableView beginUpdates];
[parentTableViewController.asyncTableView endUpdates];
}
我能够成功地重新计算单个单元格的大小并重新布局并绘制单元格以适合下载的图像。
问题: 但是,接下来我需要重新布局表格以向下或向上移动其他单元格以适应更新单元格的新大小。这就是我无法工作的地方。
尝试 #1 将重新计算表格的子视图并重新布局,但这也会重新加载所有数据,这不是我想要的。我只想重新计算和重新布局单元格,而不是重新加载所有数据。
尝试#2:使用 UITableView 时,这样做会调用 tableView:heightForRowAtIndexPath 并重新布局所有表格单元格,但不幸的是,对于 ASTableView,这会引发未实现的异常。
问题: 我如何以编程方式使 ASTableView 重新布局本身并考虑新的单元格高度?