iOS端WEEX的list组件中cell会清空subviews
2021-04-21 本文已影响0人
KeyboardDirver
前端说的list 是weexsdk在WXListComponent 中的WXTableView
继承自UITableView。
在下面方法中 调用 _unloadViewWithReusing会清空cell中的子视图
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
WXLogDebug(@"Did end displaying cell:%@, at index path:%@", cell, indexPath);
NSArray *visibleIndexPaths = [tableView indexPathsForVisibleRows];
if (![visibleIndexPaths containsObject:indexPath]) {
if (cell.contentView.subviews.count > 0) {
UIView *wxCellView = [cell.contentView.subviews firstObject];
// Must invoke synchronously otherwise it will remove the view just added.
WXCellComponent *cellComponent = (WXCellComponent *)wxCellView.wx_component;
if (cellComponent.isRecycle) {
[cellComponent _unloadViewWithReusing:YES];
}
}
}
}
- (void)_unloadViewWithReusing:(BOOL)isReusing
{
WXAssertMainThread();
if (isReusing && self->_positionType == WXPositionTypeFixed) {
return;
}
[self viewWillUnload];
_view.gestureRecognizers = nil;
[self _removeAllEvents];
if (self.ancestorScroller) {
[self.ancestorScroller removeStickyComponent:self];
[self.ancestorScroller removeScrollToListener:self];
}
for (WXComponent *subcomponents in [self.subcomponents reverseObjectEnumerator]) {
[subcomponents _unloadViewWithReusing:isReusing];
}
if ([_view superview]) {
[_view removeFromSuperview];
}
if (self->_isTemplate && self.attributes[@"@templateId"]) {
[[WXSDKManager bridgeMgr] callComponentHook:self.weexInstance.instanceId componentId:self.attributes[@"@templateId"] type:@"lifecycle" hook:@"detach" args:nil competion:nil];
}
_view = nil;
[_layer removeFromSuperlayer];
_layer = nil;
[self viewDidUnload];
}
其中isRecycle 应该是官方文档中的 cell在iOS中对应的是WXCellComponent
官方文档关于cell 的说明
经过测试 我们工程中在cell中的自定义component不会被释放只有自定义的commponent的view会被cell移除并释放
所以在自定义的component中强引用view在loadview中重新赋值即可
- (UIView *)loadView
{
if (_imageView) {
return _imageView;
}
UIIMageView* imageView = [[UIIMageView alloc] init];
imageView.weexSDKInstance = self.weexInstance;
return imageView;
}