C# MvxTableViewCell未处理

C# MvxTableViewCell未处理,c#,ios,uitableview,memory-leaks,mvvmcross,C#,Ios,Uitableview,Memory Leaks,Mvvmcross,我有一个内存泄漏问题 我基本上有一个MvxTableViewController数据绑定到一个observablecollection。我创建了一个MvxTableViewController,并将其作为子视图添加到我的控制器中 问题是,表中的单元格永远不会被释放,从而导致内存泄漏 // When the view gets removed from the stack, the cells disposed method is never called and I can see the me

我有一个内存泄漏问题

我基本上有一个MvxTableViewController数据绑定到一个observablecollection。我创建了一个MvxTableViewController,并将其作为子视图添加到我的控制器中

问题是,表中的单元格永远不会被释放,从而导致内存泄漏

// When the view gets removed from the stack, the cells disposed method is never called and I can see the memory not going away, using the Xcode Instruments tool.  The table dispose gets called as expected. but not the cell
public partial class ParticipantTableViewCell : MvxTableViewCell
{
    readonly UILabel _nameLabel = new UILabel();
    readonly UILabel _locationLabel = new UILabel();
    readonly PictureContainer _pictureView = new PictureContainer(7.0f);

    public ParticipantTableViewCell (IntPtr handle) : base(handle)
    {
        BackgroundColor = UIColor.Clear;
        SelectionStyle = UITableViewCellSelectionStyle.None;

        ContentView.AddSubviews(new UIView[] {_nameLabel, _locationLabel, _pictureView});
        _pictureView.TransparentBackground = true;

        SetupConstraints();

        // Since this view is readonly, I've removed the actual binding code
        // and instead manually init each views.
        this.DelayBind(() => {
            _nameLabel.Text = ((ParticipantViewModel)DataContext).Name;
            _locationLabel.Text = ((ParticipantViewModel)DataContext).Location;
            IsSelected = ((ParticipantViewModel)DataContext).IsSelected;
            _pictureView.AvatarImage.Image = UIImage.FromFile ("Media/" + ((ParticipantViewModel)DataContext).AvatarUrl);
        });
    }

    void SetupConstraints ()
    {
        ContentView.Subviews.ForEach(v => v.TranslatesAutoresizingMaskIntoConstraints = false);

        ContentView.AddConstraints(
            _pictureView.Height().EqualTo().HeightOf(ContentView),
            _pictureView.Width().EqualTo().HeightOf(ContentView),
            _pictureView.WithSameCenterY(ContentView),
            _pictureView.Left().EqualTo(6.0f).LeftOf(ContentView),

            _nameLabel.Left().EqualTo(10.0f).RightOf(_pictureView),
            _nameLabel.Right().EqualTo().RightOf(ContentView),
            _nameLabel.Bottom().EqualTo(2.0f).CenterYOf(ContentView),

            _locationLabel.Left().EqualTo(10.0f).RightOf(_pictureView),
            _locationLabel.Right().EqualTo().RightOf(ContentView),
            _locationLabel.Top().EqualTo(4.0f).CenterYOf(ContentView)

        );
    }

    // never called
    protected override void Dispose (bool disposing)
    {
        base.Dispose(disposing);
    }

}

    class FriendsView : MvxTableViewController
{
    protected new FriendsViewModel ViewModel { get { return (FriendsViewModel) base.ViewModel; } }

    public FriendsView ()
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad();
        TableView.BackgroundColor = UIColor.Clear;
        TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
        TableView.RowHeight = 60;
        TableView.ScrollEnabled = false;

        var source = new MvxSimpleTableViewSource(TableView, typeof(ParticipantTableViewCell));
        TableView.Source = source;

        var set = this.CreateBindingSet<FriendsView, FriendsViewModel>();
        set.Bind(source).To(vm => vm.YuFitFriends);
        set.Apply();
    }
}
//当视图从堆栈中移除时,永远不会调用cells disposed方法,我可以使用Xcode Instruments工具看到内存不会消失。按预期调用表dispose。但不是细胞
公共部分类ParticipantTableViewCell:MvxTableViewCell
{
只读UILabel _namelab=新UILabel();
只读UILabel_locationLabel=新UILabel();
只读PictureContainer _pictureView=新的PictureContainer(7.0f);
public ParticipantTableViewCell(IntPtr句柄):基本(句柄)
{
BackgroundColor=UIColor.Clear;
SelectionStyle=UITableViewCellSelectionStyle.None;
addSubview(新的UIView[]{{u namelab,{u locationLabel,{u pictureView});
_pictureView.TransparentBackground=true;
SetupConstraints();
//因为这个视图是只读的,所以我删除了实际的绑定代码
//而是手动初始化每个视图。
这个.DelayBind(()=>{
_Text=((ParticipantViewModel)DataContext).Name;
_locationLabel.Text=((ParticipantViewModel)DataContext);
IsSelected=((ParticipantViewModel)DataContext).IsSelected;
_pictureView.AvatarImage.Image=UIImage.FromFile(“媒体/”+((ParticipantViewModel)数据上下文).AvatarUrl);
});
}
无效设置约束()
{
ContentView.Subviews.ForEach(v=>v.TranslatesAutoresizingMaskIntoConstraints=false);
ContentView.AddConstraints(
_pictureView.Height().EqualTo().HeightOf(ContentView),
_pictureView.Width().EqualTo().HeightOf(ContentView),
_pictureView。使用SameCentery(ContentView),
_pictureView.Left().EqualTo(6.0f).LeftOf(ContentView),
_namelab.Left().EqualTo(10.0f).RightOf(_pictureView),
_nameLabel.Right().EqualTo().RightOf(ContentView),
_nameLabel.Bottom().EqualTo(2.0f).CenterYOf(ContentView),
_locationLabel.Left().EqualTo(10.0f).RightOf(_pictureView),
_locationLabel.Right().EqualTo().RightOf(ContentView),
_locationLabel.Top().EqualTo(4.0f).CenterYOf(ContentView)
);
}
//从未打过电话
受保护的覆盖无效处置(布尔处置)
{
基地。处置(处置);
}
}
类友视图:MvxTableViewController
{
受保护的新FriendsViewModel视图模型{get{return(FriendsViewModel)base.ViewModel;}
公众友好视图()
{
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
TableView.BackgroundColor=UIColor.Clear;
TableView.SeparatorStyle=UITableViewCellSeparatorStyle.None;
TableView.RowHeight=60;
TableView.ScrollEnabled=false;
var source=新的MvxSimpleTableViewSource(TableView,typeof(ParticipantTableViewCell));
TableView.Source=Source;
var set=this.CreateBindingSet();
set.Bind(source.To)(vm=>vm.YuFitFriends);
set.Apply();
}
}
有人知道我的手机为什么漏水吗

谢谢 拍

你认为这会被称为魔术吗?不

Dispose只是类中的另一个方法,当您不再需要它的实例时,应该调用它


此外,您还应该在此dispose方法中处理类中的所有一次性实例(例如:PictureContainer)。此外,请确保取消订阅任何活动订阅。只调用base.Dispose不会释放此类中的任何引用。

我得到了一个可行的解决方案。。。问题似乎在于MVVM如何交叉注册单元以供重用。如果我从MvxBaseTableSource而不是使用MvxSimpleTableSource派生我自己的类,并且我以不同的方式创建单元格,那么我可以正确地处理所有内容:

class MySource : MvxTableViewSource
{
    public MySource (UITableView tableView) : base(tableView)
    {

    }

    protected override UITableViewCell GetOrCreateCellFor (UITableView tableView, NSIndexPath indexPath, object item)
    {
        var cell = tableView.DequeueReusableCell (ParticipantTableViewCell.Key) as ParticipantTableViewCell;
        if (cell == null)
            cell = new ParticipantTableViewCell ();

        return cell;
    }
}
在我的tableviewcontroller中,我使用MySource

    var source = new MySource(TableView);
现在一切都好了

    var source = new MySource(TableView);