Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[iOS] Fix iOS crash that a header is inserted to list, and splitting …
Browse files Browse the repository at this point in the history
…cells calculation incorrectly. (#2548)
  • Loading branch information
wqyfavor authored and jianhan-he committed Jun 14, 2019
1 parent 8aca648 commit 6a16112
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions ios/sdk/WeexSDK/Sources/Component/WXListComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,39 @@ - (BOOL)_insertSubcomponent:(WXComponent *)subcomponent atIndex:(NSInteger)index
&& [subcomponent isKindOfClass:[WXHeaderComponent class]]) {
// insert a header in the middle, one section may divide into two
// so the original section need to be reloaded
NSIndexPath *indexPathBeforeHeader = [self indexPathForSubIndex:index - 1];
if (_sections[insertIndex - 1].rows.count != 0 && indexPathBeforeHeader.row < _sections[insertIndex - 1].rows.count - 1) {

/*
Here we may encounter a problem that _sections is not always containing all cells of list.
Because cell are not added to _sections until cellDidLayout. So if a cell is not added to _sections,
NSIndexPath *indexPathBeforeHeader = [self indexPathForSubIndex:index - 1];
The indexPathForSubIndex method use all sub components of list to calculate row in section. This would
be incorrect if a cell is not added to _sections. And the split is incorrect resulting some cells put to
wrong WXSectionComponent and then UITableView crash.
In fixed version, we use _subcomponents[index - 1] to get the last component that should be put to original section
and get the index of it in section rows.
*/

if (_sections[insertIndex - 1].rows.count > 0) {
WXComponent* componentBeforeHeader = _subcomponents[index - 1];

reloadSection = _sections[insertIndex - 1];
NSArray *rowsToSeparate = reloadSection.rows;
insertSection.rows = [[rowsToSeparate subarrayWithRange:NSMakeRange(indexPathBeforeHeader.row + 1, rowsToSeparate.count - indexPathBeforeHeader.row - 1)] mutableCopy];
reloadSection.rows = [[rowsToSeparate subarrayWithRange:NSMakeRange(0, indexPathBeforeHeader.row + 1)] mutableCopy];
NSUInteger indexOfLastComponentAfterSeparate = [rowsToSeparate indexOfObject:componentBeforeHeader];

insertSection.rows = [[rowsToSeparate subarrayWithRange:NSMakeRange(indexOfLastComponentAfterSeparate + 1, rowsToSeparate.count - (indexOfLastComponentAfterSeparate + 1))] mutableCopy];
reloadSection.rows = [[rowsToSeparate subarrayWithRange:NSMakeRange(0, indexOfLastComponentAfterSeparate + 1)] mutableCopy];
}

// This is wrong!!!
// NSIndexPath *indexPathBeforeHeader = [self indexPathForSubIndex:index - 1];
// if (_sections[insertIndex - 1].rows.count != 0 && indexPathBeforeHeader.row < _sections[insertIndex - 1].rows.count - 1) {
// reloadSection = _sections[insertIndex - 1];
// NSArray *rowsToSeparate = reloadSection.rows;
// insertSection.rows = [[rowsToSeparate subarrayWithRange:NSMakeRange(indexPathBeforeHeader.row + 1, rowsToSeparate.count - indexPathBeforeHeader.row - 1)] mutableCopy];
// reloadSection.rows = [[rowsToSeparate subarrayWithRange:NSMakeRange(0, indexPathBeforeHeader.row + 1)] mutableCopy];
// }
}

[_sections insertObject:insertSection atIndex:insertIndex];
Expand Down

0 comments on commit 6a16112

Please sign in to comment.