简体   繁体   中英

Disposing UITableView or UICollectionView cell

I have a UITableView which uses a custom TableViewCell. This TableViewCell contains an observable subscription(or it could be gesture recognizer) that needs to be disposed when the cell is disposed.

I am not clear when should I explicitly call Dispose on the table view cell. CellDisplayEnded is only called when cell is not visible due to scrolling and not when the TableView is reloaded or if TableView is on a popover and the popover gets dismissed. In any of these cases, the dispose is only called from the finalizer when the TableView itself gets disposed. But I'm hesitant to add the clean up code as the cell is already in finalized queue and the state of it is indeterministic. I found no guidance on how to handle this situation from Xamarin.

I am wondering if I should dispose all visible cells when the TableViewSource is disposed. Any thoughts?

When you initialize the custom cell, set the tag to -1 & in GetCell method use code below for checking, in order we dispose the cell that not visible & just show visible cell.

if (cell == null)
            {
                cell = new UITableViewCell(UITableViewCellStyle.Default, "identifier");
            }
            else
            {
                if(cell.ContentView!=null)
                {
                foreach (UIView subview in cell.ContentView)
                    {
                        if (subview.Tag == -1)
                        {
                            subview.RemoveFromSuperview();
                        }
                    }
                }
            }

https://stackoverflow.com/a/24121686/420175中 ,Xamarin开发人员Rolf Bjarne Kvinge建议跟踪列表中的单元格,然后在它们上手动调用Dispose()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM