wqyfavor closed pull request #1265: [WEEX-459][iOS] Fix crash if template not registered for recycle list… URL: https://github.com/apache/incubator-weex/pull/1265
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm index 551acd9f14..a55dada8fa 100644 --- a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm +++ b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm @@ -499,12 +499,18 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell // 1. get the data relating to the cell id data = [_dataManager dataAtIndex:indexPath.row]; - // 2. get the template type specified by data + // 2. get the template type specified by data, and if template is not found, return an empty view of any template to avoid crash. NSString * templateType = [self templateType:indexPath]; _templateManager.collectionView = collectionView; - if (!templateType) { - WXLogError(@"Each data should have a value for %@ to indicate template type", _templateSwitchKey); - return nil; + if (!templateType || (templateType && ![_templateManager isTemplateRegistered:templateType])) { + WXLogError(@"Template %@ not registered for collection view.", templateType); + UICollectionViewCell *cellView = [_collectionView dequeueReusableCellWithReuseIdentifier:[_templateManager anyRegisteredTemplate] forIndexPath:indexPath]; + for (UIView *view in cellView.contentView.subviews) { + [view removeFromSuperview]; + } + cellView.wx_component = nil; + [cellView setAccessibilityIdentifier:nil]; + return cellView; } // 3. dequeue a cell component by template type @@ -607,7 +613,7 @@ - (NSString*)templateType:(NSIndexPath*)indexPath return templateType; } - if (_templateSwitchKey &&data[_templateSwitchKey]){ + if (_templateSwitchKey && data[_templateSwitchKey]){ templateType = data[_templateSwitchKey]; } else if (data[WXDefaultRecycleTemplateType]){ // read the default type. diff --git a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.h b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.h index 926c2f1a08..b95612230a 100644 --- a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.h +++ b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.h @@ -32,4 +32,8 @@ - (WXCellSlotComponent *)topTemplate; +- (BOOL)isTemplateRegistered:(NSString *)aTemplate; + +- (NSString *)anyRegisteredTemplate; + @end diff --git a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.m b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.m index 199d551569..2a90eebe16 100644 --- a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.m +++ b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListTemplateManager.m @@ -31,12 +31,14 @@ @implementation WXReusableCollectionViewCell @implementation WXRecycleListTemplateManager { + NSMutableSet<NSString *> *_registeredTemplates; NSMapTable<NSString *, WXCellSlotComponent *> *_templateTypeMap; } - (instancetype)init { if (self = [super init]) { + _registeredTemplates = [NSMutableSet set]; _templateTypeMap = [NSMapTable strongToWeakObjectsMapTable]; } @@ -87,7 +89,7 @@ - (WXCellSlotComponent *)templateWithType:(NSString *)type - (void)_registerCellClassForReuseID:(NSString *)templateID { WXLogDebug(@"register cell class for template id:%@", templateID); - //TODO: register class update TemplateId + [_registeredTemplates addObject:templateID]; [_collectionView registerClass:[WXReusableCollectionViewCell class] forCellWithReuseIdentifier:templateID]; } @@ -103,4 +105,16 @@ - (WXCellSlotComponent *)topTemplate return cellTemplate; } +- (BOOL)isTemplateRegistered:(NSString *)aTemplate +{ + WXAssertMainThread(); + return [_registeredTemplates containsObject:aTemplate]; +} + +- (NSString *)anyRegisteredTemplate +{ + WXAssertMainThread(); + return [_registeredTemplates anyObject]; +} + @end ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services