@@ -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