c# - iOS TableView 按钮被多次调用

标签 c# ios uitableview xamarin xamarin.ios

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

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

我知道问题是单元格被重复使用,所以这就是按钮被多次调用的原因,但我找不到阻止它的方法。

我通过代码添加了控制台写行语句,并且永远不会调用 MoveToWindow 中的 else 部分。这可能是原因吗?

解决方案的研究资料:

my code is calling twice the btndelete method in uitableview

UIButton click event getting called multiple times inside custom UITableViewCell

我的代码:

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

请注意,我在 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

关于c# - iOS TableView 按钮被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35934082/

相关文章:

ios - 如何在 UITableView 中添加背景视频并使其在滚动 UITableViewCells 时固定?

C# 静态类和 is 运算符

c# - 是否可以在 Controller 中使用 UpdateModel 来填充对象?

objective-c - 通过蓝牙发送文件

ios - 如何将 View 发送到后台,使其位于主视图后面?

ios - 安装后从包中删除文件

c# - SuppressUnmanagedCodeSecurity 还是 SuppressUnmanagedCodeSecurityAttribute?

c# - 即使虚拟目录允许匿名访问,Web 服务也会出现 "HTTP status 401: Access Denied"错误

ios - 从另一种方法 iOS 更新 UITableViewCell 中的值

ios - 在iOS模拟器中检测UIButton上的长按