iOS TableView按钮被多次调用

5

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

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

我知道问题在于单元格被重用,因此按钮被多次调用,但我找不到防止这种情况发生的方法。

我在整个代码中添加了控制台写入语句,但是在MoveToWindow中的else部分从未被调用。那可能是原因吗?

解决方案的研究材料:

我的代码在UITableview中两次调用btndelete方法

自定义UITableViewCell中的UIButton点击事件被多次调用

我的代码:

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;
            }
        }
    }
}
1个回答

7

iOS中会重复使用cell,因此在重复使用之前,需要确保正确解除处理程序并重置状态。您可以像这样进行操作:

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 ();
}

注意我在PrepareForReuse覆盖方法中解除事件处理程序的挂钩。这是正确的清理和重置单元格以供重用的位置。你不应该使用MovedToWindow()
然后,你的GetCell方法将如下所示:
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;
}

_likeButtonHandler 是一个简单的 EventHandler


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接