C# 正在多次调用iOS TableView按钮

C# 正在多次调用iOS TableView按钮,c#,ios,uitableview,xamarin,xamarin.ios,C#,Ios,Uitableview,Xamarin,Xamarin.ios,我有一个带有自定义UITableViewCell的TableView。在每个单元格中,我都有多个按钮,当向下和向上滚动后单击任何按钮时,它会调用自己,我会多次向下和向上滚动 我已经阅读并研究了解决方案,但还没有找到解决方案 我知道问题是电池被重复使用,这就是为什么按钮被多次调用的原因,但我找不到防止它的方法 我通过代码添加了控制台写行语句,MoveToWindow中的else部分是never调用。这就是原因吗 解决方案的研究材料: 我的代码: namespace Class.iOS {

我有一个带有自定义UITableViewCell的TableView。在每个单元格中,我都有多个按钮,当向下和向上滚动后单击任何按钮时,它会调用自己,我会多次向下和向上滚动

我已经阅读并研究了解决方案,但还没有找到解决方案

我知道问题是电池被重复使用,这就是为什么按钮被多次调用的原因,但我找不到防止它的方法

我通过代码添加了控制台写行语句,MoveToWindow中的else部分是never调用。这就是原因吗

解决方案的研究材料:

我的代码:

namespace Class.iOS
{
    public partial class CustomCell : UITableViewCell
    {
        public static readonly NSString Key = new NSString ("CustomCell");
        public static readonly UINib Nib;
        public int Row { get; set; }
        public event EventHandler LikeButtonPressed;

        private void OnLikeButtonPressed()
        {
            if (LikeButtonPressed != null)
            {
                LikeButtonPressed(this, EventArgs.Empty);
            }
        }

        public override void MovedToWindow()
        {
            if (Window != null)
            {
                btnAdd.TouchUpInside += HandleLikeButtonPressed;
            }
            else
            {
                btnAdd.TouchUpInside -= HandleLikeButtonPressed;
            }
        }

        private void HandleLikeButtonPressed(object sender, EventArgs e)
        {
            OnLikeButtonPressed();
        }

        static CustomCell ()
        {
            Nib = UINib.FromName ("CustomCell", NSBundle.MainBundle);
        }

        public CustomCell ()
        {           
        }

        public CustomCell (IntPtr handle) : base (handle)
        {           
        }

        public void UpdateCell (string Name, int number)
        {
            // some code
        }

        public class TableSource : UITableViewSource
        {           
            public override nint RowsInSection (UITableView tableview, nint section)
            {
                return 8;
            }

            private void HandleLikeButtonPressed(object sender, EventArgs e)
            {
                var cell = (CustomCell)sender;
                var row = cell.Row;

                switch (row) 
                {
                case 0:
                    cell.label.Text = ""
                    break;
                case 1:
                    cell.label.Text = ""
                    break;
                }
            }
            public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
            {
                var cell = tableView.DequeueReusableCell (CustomCell.Key) as CustomCell;
                cell.Row = indexPath.Row;

                if (cell == null) 
                {
                    cell = new CustomCell ();
                    var views = NSBundle.MainBundle.LoadNib("CustomCell", cell, null);
                    cell.LikeButtonPressed += HandleLikeButtonPressed;
                    cell = Runtime.GetNSObject( views.ValueAt(0) ) as CustomCell;

                }

                cell.UpdateCell 
                (
                    // some code
                );

                return cell;
            }
        }
    }
}

这些单元格在iOS中被重用,因此您需要确保在重用单元格时正确地解除挂钩处理程序并重置状态。您可以这样做:

public partial class CustomCell : UITableViewCell {

EventHandler _likeButtonHandler;

public static readonly NSString Key = new NSString (typeof(CustomCell).Name);
public static readonly UINib Nib = UINib.FromName (typeof(CustomCell).Name, NSBundle.MainBundle);

public CustomCell ()
{

}

public CustomCell (IntPtr handle) : base (handle)
{
}

public override void PrepareForReuse ()
{
    LikeButton.TouchUpInside -= _likeButtonHandler;
    _likeButtonHandler = null;

    base.PrepareForReuse ();
}

public void SetupCell (int row, string Name, EventHandler likeBtnHandler)
{
    _likeButtonHandler = likeBtnHandler;

    LikeButton.TouchUpInside += _likeButtonHandler;
    LikeButton.Tag = row;

    NameLabel.Text = Name;
    RowLabel.Text = row.ToString ();
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    var cell = tableView.DequeueReusableCell (CustomCell.Key) as CustomCell ?? new CustomCell ();

    cell.SetupCell (indexPath.Row, _fruits [indexPath.Row], _likeButtonHandler);

    return cell;
}
请注意,我在
PrepareForReuse
override中取消了事件处理程序的挂钩。这是进行清理和重置单元以供重复使用的正确位置。您不应该使用
MovedToWindow()

然后您的
GetCell
方法将如下所示:

public partial class CustomCell : UITableViewCell {

EventHandler _likeButtonHandler;

public static readonly NSString Key = new NSString (typeof(CustomCell).Name);
public static readonly UINib Nib = UINib.FromName (typeof(CustomCell).Name, NSBundle.MainBundle);

public CustomCell ()
{

}

public CustomCell (IntPtr handle) : base (handle)
{
}

public override void PrepareForReuse ()
{
    LikeButton.TouchUpInside -= _likeButtonHandler;
    _likeButtonHandler = null;

    base.PrepareForReuse ();
}

public void SetupCell (int row, string Name, EventHandler likeBtnHandler)
{
    _likeButtonHandler = likeBtnHandler;

    LikeButton.TouchUpInside += _likeButtonHandler;
    LikeButton.Tag = row;

    NameLabel.Text = Name;
    RowLabel.Text = row.ToString ();
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    var cell = tableView.DequeueReusableCell (CustomCell.Key) as CustomCell ?? new CustomCell ();

    cell.SetupCell (indexPath.Row, _fruits [indexPath.Row], _likeButtonHandler);

    return cell;
}
类似于按钮处理程序的
是一个简单的
EventHandler