gpt4 book ai didi

ios - Xamarin UIButton 有多个事件处理程序

转载 作者:行者123 更新时间:2023-11-29 11:40:28 26 4
gpt4 key购买 nike

我遇到了一个问题,单元格中的 UIButtons 在 Xamarin 中分配了多个事件处理程序。有没有办法确保只分配一个事件处理程序?我假设在重新加载 tableview 或 collectionview 时,这些单元格中的任何内容都会被释放/重置。这是不正确的吗?

这是我在委托(delegate)中设置单元格的地方:

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
EventHandler clickTarget = (sender, e) =>
{
if (this.parantController.showCategories)
{
this.parantController.apiCallGetBusinessesIndustries(this.parantController.businessesCategoriesResponseModel.business_categories[indexPath.Row].id);
this.parantController.showIndustries = true;
this.parantController.showCategories = false;
}
else if (this.parantController.showIndustries)
{
this.parantController.apiCallGetBusinessessList("", this.parantController.businessesIndustriesResponseModel.business_industries[indexPath.Row].id);
this.parantController.filterView.Hidden = true;
this.parantController.showVerticalList = false;
this.parantController.showIndustries = false;
this.parantController.clearAll = true;
this.parantController.filterCollectionView.ReloadData();
this.parantController.clearAll = false;
//this.parantController.showCategories = true;
}
};

var cell = (FilterCollectionCell)collectionView.DequeueReusableCell("filterCollectionCell", indexPath);
if (this.parantController.businessesCategoriesResponseModel.business_categories != null && !this.parantController.showIndustries)
{
cell.updateCell(false, this.parantController.businessesCategoriesResponseModel.business_categories[indexPath.Row].name, clickTarget);
}
if (this.parantController.showIndustries)
{
cell.updateCell(false, this.parantController.businessesIndustriesResponseModel.business_industries[indexPath.Row].name, clickTarget);
}

return cell;
}

这是我将事件处理程序添加到单元格 View Controller 中的按钮的代码:

public void updateCell(Boolean selectedFilterType, string title, EventHandler clickHandler) {

btnFilter.RemoveTarget(this, null, UIControlEvent.AllEvents);

btnFilter.AddTarget(clickHandler, UIControlEvent.TouchUpInside);// = handlerToUse;

this.btnFilter.SetTitle(title, UIControlState.Normal);
}

最佳答案

您的 FilterCollectionCell 创建应该处理将事件应用于单元格,因此当重复使用单元格时,您不会每次都在 GetCell 方法中附加新事件。

否则,您可以检索所有 UButton 的目标并在每次请求单元格时删除它们:

foreach (var target in button.AllTargets)
{
button.RemoveTarget(target, null, UIControlEvent.AllTouchEvents);
}

关于ios - Xamarin UIButton 有多个事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940160/

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