问题描述
我有一个自定义的 UITableViewCell
,其中有一个 UIButton
.单击按钮时,单击事件会被多次调用.这是我正在使用的代码.
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 单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!