自定义UITableViewCell中多次调用UIButton单击事件 [英] UIButton click event getting called multiple times inside custom UITableViewCell
问题描述
我有一个自定义的 UITableViewCell
,其中包含一个 UIButton
.单击按钮后,将多次调用click事件.这是我正在使用的代码.
I have a custom UITableViewCell
which has an UIButton
in it. When the button is clicked the click event is getting called multiple times. Here is the code what i am using.
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;
}
有人知道为什么会这样吗?我可以正确地重复使用细胞吗?
Any idea why is this happening? am i reusing the cells properly?
谢谢.
推荐答案
我相信您不应每次都仅在创建新单元格时调用cell.BindData().否则,您每次重新使用单元时都将运行它.
I believe you should not call cell.BindData() every time, only when you create a new cell. Otherwise you will be running it every time you re-use your cell.
分离绑定数据内容...拔出触摸按钮
separate the bind data stuff... pull out the button touch
internal void BindData()
{
//some code
}
然后将按钮内容放入此处.
and then put the button stuff in here.
var cell = tableView.DequeueReusableCell(CustomCell.Key) as CustomCell;
if (cell == null)
{
cell = CustomCell.Create ()
cell.btnSave.TouchUpInside+= (object sender, EventArgs e) =>
{
Console.WriteLine("button clicked");
};
}
cell.BindData ();
这篇关于自定义UITableViewCell中多次调用UIButton单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!