Skip to content

Commit ce4102b

Browse files
authored
fix(ios): never re-attach torn-down ListView item views (#11642)
When a cell leaves the table, ListView tears its item view down but kept the cell's WeakRef to it and left the dead native root in the cell's contentView. A later prepare of that cell re-attached the torn-down view, whose graph was no longer rooted, so native parts of it (such as the UIColor cached by a CSS Color) could already be released; the resulting exception made the data source return a nil cell and UIKit abort. UIKit also puts removed cells back into the table without asking for them again, and those rows stayed blank. Both happen constantly while an accessibility client (XCUITest, VoiceOver) disables cell reuse. - detach the item's native root from the cell on teardown - clear the cell's reference so the next prepare builds a new view - prepare a cell that is back in the table without content, from didMoveToSuperview and from willDisplay; cells UIKit announces after taking them out of the table are left alone, since preparing them would keep them in the ListView's map for good - the same teardown changes for sticky header views
1 parent 2a41eb1 commit ce4102b

2 files changed

Lines changed: 91 additions & 16 deletions

File tree

‎apps/automated/src/ui/list-view/list-view-tests.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,37 @@ export class ListViewTest extends UITest<ListView> {
177177
}
178178
}
179179

180+
// UIKit can take a cell out of the table and display it again without asking for it in
181+
// tableView:cellForRowAtIndexPath: (constantly while an accessibility client disables cell reuse).
182+
public test_cell_displayed_again_after_teardown_gets_new_content_ios() {
183+
if (__APPLE__) {
184+
const listView = this.testView;
185+
listView.on(ListView.itemLoadingEvent, this.loadViewWithItemNumber);
186+
listView.items = FEW_ITEMS;
187+
this.waitUntilListViewReady();
188+
189+
const tableView = <UITableView>listView.ios;
190+
const indexPath = NSIndexPath.indexPathForRowInSection(1, 0);
191+
const cell = <any>tableView.cellForRowAtIndexPath(indexPath);
192+
const tableContent = cell.superview;
193+
const tornDownView = cell.view;
194+
TKUnit.assertNotNull(tornDownView, 'cell has an item view');
195+
196+
cell.removeFromSuperview();
197+
TKUnit.assertEqual(cell.contentView.subviews.count, 0, 'the torn-down item view is detached from the cell');
198+
TKUnit.assertNull(cell.view, 'the cell no longer refers to the torn-down item view');
199+
200+
tableView.delegate.tableViewWillDisplayCellForRowAtIndexPath(tableView, cell, indexPath);
201+
TKUnit.assertNull(cell.view, 'a cell outside the table is not prepared');
202+
203+
tableContent.addSubview(cell);
204+
TKUnit.assertEqual(cell.contentView.subviews.count, 1, 'the cell has exactly one item view again');
205+
TKUnit.assertNotEqual(cell.view, tornDownView, 'the torn-down item view is not re-attached');
206+
TKUnit.assertEqual(cell.view.parent, listView, 'the new item view is a child of the ListView');
207+
TKUnit.assertEqual((<Label>cell.view).text, 'item 1', 'the new item view shows the row');
208+
}
209+
}
210+
180211
public test_set_items_to_array_creates_native_views() {
181212
var listView = this.testView;
182213
listView.on(ListView.itemLoadingEvent, this.loadViewWithItemNumber);

‎packages/core/ui/list-view/index.ios.ts‎

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,25 @@ class ListViewCell extends UITableViewCell {
6060
}
6161
}
6262

63+
public didMoveToSuperview(): void {
64+
super.didMoveToSuperview();
65+
66+
// UIKit can put a cell back into the table without asking for it in tableView:cellForRowAtIndexPath:.
67+
const listView = this.listView?.deref();
68+
if (this.superview && listView && !listView._hasCellContent(this)) {
69+
const indexPath = (listView.nativeViewProtected as UITableView)?.indexPathForCell(this);
70+
if (indexPath) {
71+
listView._prepareCellForDisplay(this, indexPath);
72+
}
73+
}
74+
}
75+
6376
public get view(): View {
6477
return this.owner ? this.owner.deref() : null;
6578
}
6679

6780
public owner: WeakRef<View>;
81+
public listView: WeakRef<ListView>;
6882
}
6983

7084
@NativeClass
@@ -166,18 +180,7 @@ class DataSource extends NSObject implements UITableViewDataSource {
166180
if (owner) {
167181
const template = owner.sectioned ? owner._getItemTemplateInSection(indexPath.section, indexPath.row) : owner._getItemTemplate(indexPath.row);
168182
cell = <ListViewCell>(tableView.dequeueReusableCellWithIdentifier(template.key) || ListViewCell.initWithEmptyBackground());
169-
owner._prepareCell(cell, indexPath);
170-
171-
const cellView: View = cell.view;
172-
if (cellView && cellView.isLayoutRequired) {
173-
// Arrange cell views. We do it here instead of _layoutCell because _layoutCell is called
174-
// from 'tableViewHeightForRowAtIndexPath' method too (in iOS 7.1) and we don't want to arrange the fake cell.
175-
const width = layout.getMeasureSpecSize(owner.widthMeasureSpec);
176-
const rowHeight = owner._effectiveRowHeight;
177-
const cellHeight = rowHeight > 0 ? rowHeight : owner.getHeight(indexPath.row, indexPath.section);
178-
cellView.iosOverflowSafeAreaEnabled = false;
179-
View.layoutChild(owner, cellView, 0, 0, width, cellHeight);
180-
}
183+
owner._prepareCellForDisplay(cell, indexPath);
181184
} else {
182185
cell = <ListViewCell>ListViewCell.initWithEmptyBackground();
183186
}
@@ -204,6 +207,11 @@ class UITableViewDelegateImpl extends NSObject implements UITableViewDelegate {
204207

205208
public tableViewWillDisplayCellForRowAtIndexPath(tableView: UITableView, cell: UITableViewCell, indexPath: NSIndexPath) {
206209
const owner = this._owner?.deref();
210+
// UIKit also announces cells it has already taken out of the table and will drop; preparing
211+
// those would keep them and their item views in the ListView's map for good.
212+
if (owner && cell instanceof ListViewCell && cell.superview && !owner._hasCellContent(cell)) {
213+
owner._prepareCellForDisplay(cell, indexPath);
214+
}
207215
if (owner && indexPath.row === owner.items.length - 1) {
208216
owner.notify(<EventData>{
209217
eventName: LOADMOREITEMS,
@@ -315,6 +323,11 @@ class UITableViewRowHeightDelegateImpl extends NSObject implements UITableViewDe
315323

316324
public tableViewWillDisplayCellForRowAtIndexPath(tableView: UITableView, cell: UITableViewCell, indexPath: NSIndexPath) {
317325
const owner = this._owner?.deref();
326+
// UIKit also announces cells it has already taken out of the table and will drop; preparing
327+
// those would keep them and their item views in the ListView's map for good.
328+
if (owner && cell instanceof ListViewCell && cell.superview && !owner._hasCellContent(cell)) {
329+
owner._prepareCellForDisplay(cell, indexPath);
330+
}
318331
if (owner && indexPath.row === owner.items.length - 1) {
319332
owner.notify(<EventData>{
320333
eventName: LOADMOREITEMS,
@@ -867,8 +880,6 @@ export class ListView extends ListViewBase {
867880
if (!cell.view) {
868881
cell.owner = new WeakRef(view);
869882
} else if (cell.view !== view) {
870-
// Remove view from super view now as nativeViewProtected will be null afterwards
871-
(<UIView>cell.view.nativeViewProtected)?.removeFromSuperview();
872883
this._removeContainer(cell);
873884
cell.owner = new WeakRef(view);
874885
}
@@ -882,6 +893,9 @@ export class ListView extends ListViewBase {
882893
view._listViewItemIndex = indexPath.row;
883894
}
884895
this._map.set(cell, view);
896+
if (cell.listView?.deref() !== this) {
897+
cell.listView = new WeakRef(this);
898+
}
885899

886900
// We expect that views returned from itemLoading are new (e.g. not reused).
887901
if (view && !view.parent) {
@@ -897,8 +911,33 @@ export class ListView extends ListViewBase {
897911
return cellHeight;
898912
}
899913

914+
public _hasCellContent(cell: ListViewCell): boolean {
915+
const view = cell.view;
916+
917+
return !!view && this._map.get(cell) === view;
918+
}
919+
920+
public _prepareCellForDisplay(cell: ListViewCell, indexPath: NSIndexPath): void {
921+
this._prepareCell(cell, indexPath);
922+
923+
const cellView: View = cell.view;
924+
if (cellView && cellView.isLayoutRequired) {
925+
// Arrange cell views. We do it here instead of _layoutCell because _layoutCell is called
926+
// from 'tableViewHeightForRowAtIndexPath' method too (in iOS 7.1) and we don't want to arrange the fake cell.
927+
const width = layout.getMeasureSpecSize(this.widthMeasureSpec);
928+
const rowHeight = this._effectiveRowHeight;
929+
const cellHeight = rowHeight > 0 ? rowHeight : this.getHeight(indexPath.row, indexPath.section);
930+
cellView.iosOverflowSafeAreaEnabled = false;
931+
View.layoutChild(this, cellView, 0, 0, width, cellHeight);
932+
}
933+
}
934+
900935
public _removeContainer(cell: ListViewCell): void {
901936
const view: ItemView = cell.view;
937+
// ListView does not detach its children's native views, and nativeViewProtected is gone after _removeView.
938+
const root = view.parent instanceof ListView ? view : view.parent;
939+
(root.nativeViewProtected as UIView)?.removeFromSuperview();
940+
902941
// This is to clear the StackLayout that is used to wrap ProxyViewContainer instances.
903942
if (!(view.parent instanceof ListView)) {
904943
this._removeView(view.parent);
@@ -912,6 +951,9 @@ export class ListView extends ListViewBase {
912951
view._listViewSectionIndex = undefined;
913952
this._preparingCell = preparing;
914953
this._map.delete(cell);
954+
// Drop the reference so the next prepare builds a new view rather than reviving this one: its graph
955+
// is no longer rooted, so native parts of it (such as cached colors) may already be released.
956+
cell.owner = undefined;
915957
}
916958

917959
public _prepareHeader(headerCell: ListViewHeaderCell, section: number): number {
@@ -936,8 +978,6 @@ export class ListView extends ListViewBase {
936978
if (!headerCell.view) {
937979
headerCell.owner = new WeakRef(view);
938980
} else if (headerCell.view !== view) {
939-
// Remove old view and set new one
940-
(<UIView>headerCell.view.nativeViewProtected)?.removeFromSuperview();
941981
this._removeHeaderContainer(headerCell);
942982
headerCell.owner = new WeakRef(view);
943983
}
@@ -1058,6 +1098,9 @@ export class ListView extends ListViewBase {
10581098

10591099
public _removeHeaderContainer(headerCell: ListViewHeaderCell): void {
10601100
const view: View = headerCell.view;
1101+
const root = view.parent instanceof ListView ? view : view.parent;
1102+
(root.nativeViewProtected as UIView)?.removeFromSuperview();
1103+
10611104
// This is to clear the StackLayout that is used to wrap ProxyViewContainer instances.
10621105
if (!(view.parent instanceof ListView)) {
10631106
this._removeView(view.parent);
@@ -1069,6 +1112,7 @@ export class ListView extends ListViewBase {
10691112
view.parent._removeView(view);
10701113
this._preparingHeader = preparing;
10711114
this._headerMap.delete(headerCell);
1115+
headerCell.owner = undefined;
10721116
}
10731117

10741118
[separatorColorProperty.getDefault](): UIColor {

0 commit comments

Comments
 (0)