2016-08-15 12 views
2

My TableViewセルにボタンがあります。クリックすると、セルを展開します。展開するには、ボタンをクリックして行の高さを変更して行をリロードします。しかし、ある行が展開され、次に別の行を展開しようとすると、その動作は期待通りにはなりません。クリックされた行ではなく、以前に展開された行が閉じます。ios:UITableVIewCellが期待どおりに機能しない

また、時にはボタンを2回クリックして展開する必要があります。

public class ProgramMeasurementDetailsTableViewSource : UITableViewSource 
{ 

    List<ProgramMeasurementDetail> mList; 

    string HeaderIdentfier = "programMeasurementDetailsHeader"; 
    string CellIdentifier = "programMeasurementDetailsCell"; 
    int TAG_CHECKBOX = 61; 
    int TAG_LABEL_VITAL_NAME = 62; 
    int TAG_LABEL_GOAL = 63; 
    int TAG_BUTTON_EDIT = 64; 
    int TAG_VIEW_HEADER_COLUMN_SEPARATOR = 66; 
    int TAG_VIEW_ROW_COLUMN_SEPARATOR = 65; 
    List<bool> expandStatusList = new List<bool>(); 
    UITableView tableView; 

    public ProgramMeasurementDetailsTableViewSource(List<ProgramMeasurementDetail> mList, UITableView tableView) 
    { 
     this.tableView = tableView; 
     this.mList = mList; 
     for (int i = 0; i < mList.Count; i++) 
      expandStatusList.Add(false); 
    } 

    public override nint NumberOfSections(UITableView tableView) 
    { 
     return 1; 
    } 

    public override nint RowsInSection(UITableView tableview, nint section) 
    { 
     return mList.Count; 
    } 

    public override nfloat GetHeightForHeader(UITableView tableView, nint section) 
    { 
     return 32; 
    } 

    public override nfloat GetHeightForRow(UITableView tableView, Foundation.NSIndexPath indexPath) 
    { 
     if (expandStatusList[indexPath.Row]) 
      return 70; 
     else 
      return 32; 
    } 

    public override UITableViewCell GetCell(UITableView tableView, Foundation.NSIndexPath indexPath) 
    { 
     UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier); 
     if (cell == null) 
     { 
      cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); 
     } 
     CheckBox checkBox = (CheckBox)cell.ViewWithTag(TAG_CHECKBOX); 
     UILabel vitalNameLabel = (UILabel)cell.ViewWithTag(TAG_LABEL_VITAL_NAME); 
     UILabel goalLabel = (UILabel)cell.ViewWithTag(TAG_LABEL_GOAL); 
     UIButton editGoalButton = (UIButton)cell.ViewWithTag(TAG_BUTTON_EDIT); 
     editGoalButton.TouchUpInside += (object sender, EventArgs e) => 
     { 
      expandStatusList[indexPath.Row] = !expandStatusList[indexPath.Row]; 
      tableView.ReloadRows(new Foundation.NSIndexPath[] { indexPath }, UITableViewRowAnimation.Automatic); 

     }; 

     ..... 
     return cell; 
    } 
} 

私は、その後、IF(セル== null)の内部での取り扱いボタンクリックイベントを移動しようとしたが、ボタンをクリック/拡大がまったく機能していません。ブレークポイントを置くと、その部分は決して実行されないようです。

私はストーリーボードでセルレイアウトを設計しました。

私は今この問題にかなり苦しんでいます。どんな助けもありがとうございます。

答えて

2

リストを上下にスクロールしたときに表示されるバグはありますか?これは、GetCellメソッドのTouchUpInsideイベントハンドラを配線しているので、セルが再利用されてもフックが解除されることはないため、同じハンドラまたは複数のハンドラに接続された複数のセルになります。

一般にGetCellメソッドではこれらのタイプを使用したくないので、DequeueReusableCellにのみ使用するか、新しいタイプを作成してください。他のすべてはカスタムセル内で行う必要があります。

あなたのコードの例を見てみると、それはあなたがカスタムセルを使用しているように見えるので、何を行う可能性がこれではありません。GetCell

1.)の行として、ボタンのタグを設定します

private void ToggleRow(object sender, EventArgs e) 
{ 
    var btn = sender as UIButton; 
    expandStatusList[btn.Tag] = !expandStatusList[btn.Tag]; 
    tableView.ReloadRows(new Foundation.NSIndexPath[] { NSIndexPath.FromRowSection(btn.Tag, 0) }, UITableViewRowAnimation.Automatic); 
} 

3)GetCell国連で:ProgramMeasurementDetailsTableViewSourceでIndexPath

editGoalButton.Tag = indexPath.Row; 

2)あなたのイベントハンドラのための別の方法を作成します。

editGoalButton.TouchUpInside -= ToggleRow; 
editGoalButton.TouchUpInside += ToggleRow; 

このボタンは一度だけフックアップされていることを確認しますが、それは本当にこのような状況を処理するための正しい方法ではありません:あなたはTouchUpInsideハンドラをrehook前にフック。カスタムセルを使用し、セル内のすべての配線を行ってから、PrepareForReuseオーバーライドでアンフックとクリーンアップを行い、UITableViewCellにする必要があります。私は非常にこのを行くことをお勧めしますXamarin tutorial

あなたがそれを通過する場合...どのようにGetCell方法が統合されていることに注意してください。あなたのCustomCellクラスで

--UPDATE--

private EventHandler _tapAction; 
public void Setup(EventHandler tapAction, int row, ....) 
{ 
    //keep a reference to the action, so we can unhook it later 
    _tapAction = tapAction; 
    editGoalButton.Tag = row; 
    .... 
    editGoalButton.TouchUpInside += _tapAction; 
} 

public override void PrepareForReuse() 
{ 
    .... 
    editGoalButton.TouchUpInside -= _tapAction; 
    _tapAction = null; 
} 

そして、あなたはGetCellSetupを呼び出すときにあなただけのアクションパラメータとしてToggleRow方法を通過します。

+0

私はストーリーボードを使用して細胞を設計すべきではありませんか?今私はストーリーボードで設計されたセルを持っています。 CustomCellクラスでセルのデザインとして使用することは可能ですか? – HeisenBerg

+0

ストーリーボードのセルも使用できます。 'TableView Cell Style'が' Custom'に設定されていることを確認し、クラス名を付けてください。 Xamarinはカスタムクラス用の '.cs'ファイルを生成します。このチュートリアルの最初の部分では、コードビハインドファイルを生成する方法を説明します。https://developer.xamarin.com/guides/ios/user_interface/tables/autosizing-row-height/ – pnavk

+0

ここでは、TouchUpinsideボタンを定義しますカスタムセルクラスを使用して?カスタムセルクラスに入れますか?はいの場合は、そこからテーブルビュー行をリロードするにはどうすればよいですか? – HeisenBerg

関連する問題