2017-10-25 3 views
0

Xamarin内で複数のイベントハンドラが割り当てられているセル内でUIButtonの問題が発生しています。イベントハンドラが1つしか割り当てられないようにする方法はありますか?私は、それらのセル内の何かがリリース/リセットされると、tableviewまたはcollectionviewをリロードすると仮定しました。それは間違っていますか?ここでXamarin UIButtonには複数のイベントハンドラがあります

は、デリゲートでどこのセットアップセルを次のとおりです。ここで

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; 
     } 

は、私は、セルのビューコントローラのボタンにイベントハンドラを追加するコードです:

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); 
    } 

答えて

0

あなたFilterCollectionCellセルにイベントを適用する処理を行う必要があります。したがって、セルが再利用されたときには、毎回GetCellメソッド内に新しいイベントが追加されることはありません。

そうしないと、すべてのUButtons'のターゲットを取得し、それらにセルが要求されるたびに削除することができます。

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

私はFilterCollectionCellで適用するイベントを処理するでしょうどのように? – linuxer

+0

@SushiHanoverガイダンスはありますか? – linuxer

関連する問題