ios - UIButton 单击事件在自定义 UITableViewCell 内多次调用

标签 ios uitableview xamarin.ios xamarin

我有一个自定义的UITableViewCell,其中有一个UIButton。单击按钮时,单击事件将被多次调用。这是我正在使用的代码。

CustomCell.cs

public static CustomCell Create ()
{
    return ( CustomCell ) Nib.Instantiate ( null , null ) [0];
}

internal void BindData()
{
    //some code

    btnSave.TouchUpInside+= (object sender, EventArgs e) => 
    {
        Console.WriteLine("button clicked");
    };
}

TableSource.cs

public override UITableViewCell GetCell (UITableView tableView,NSIndexPath indexPath)
{
    CustomCell cell = tableView.DequeueReusableCell ( CustomCell.Key ) as CustomCell ??  CustomCell.Create ();
    cell.BindData ();
    return cell;
}

知道为什么会发生这种情况吗?我是否正确地重复使用了细胞?

谢谢。

最佳答案

它的迟回复我遇到了同样的问题并解决如下,可能对其他人有用:

cell.tnSave.TouchUpInside -= handler; 
cell.tnSave.TouchUpInside += handler;

这将防止多次向按钮的 touchupinsider 事件添加相同的处理程序。处理程序可以定义为:

void handler(Object sender, EventArgs args)
        {
            Console.WriteLine("button clicked");
        }

关于ios - UIButton 单击事件在自定义 UITableViewCell 内多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25498047/

相关文章:

ios - 每次打开应用程序时刷新 View 和数据

ios - 从代码访问 TableView 标题

ios - xamarin.ios 上的 UITextViewL 链接

iphone - 如何在 MonoTouch 中将 NSHttpCookie 转换为 System.Net.Cookie?

ios - Xamarin、MVVM交叉和崩溃报告

iphone - iOS - 如何检查 NSOperation 是否在 NSOperationQueue 中?

iOS 应用程序图像/图标位深度

ios - 如何使用 xcarchive 对崩溃文件进行符号化

iOS-如何使用动画隐藏/显示 UITabBarController 标签栏?

ios - 无法获取tableView的contentOffset