OGeek|极客世界-中国程序员成长平台

标题: ios - 在自定义 UITableViewCell 中多次调用 UIButton 单击事件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:57
标题: ios - 在自定义 UITableViewCell 中多次调用 UIButton 单击事件

我有一个自定义的 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;
}

知道为什么会这样吗?我是否正确地重复使用了细胞?

谢谢。



Best Answer-推荐答案


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

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

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

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4