gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:42:29 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com