2016-08-15 35 views
2

我的TableView单元格有一个按钮。点击时,我想扩展单元格。为了展开,我将更改单击按钮时单元格的高度并重新加载该行。但是,当一行被扩展,然后我尝试扩展另一行时,行为不像预期的那样。先前展开的行关闭而不是点击行。ios:UITableVIewCell展开不能按预期工作

此外,有时我必须点击按钮两次才能展开。

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

我试着移动按钮单击事件,如果(细胞== NULL)的内部处理,然后但按一下按钮/ Expand是不工作的。我把断点,似乎这部分从来没有得到执行。

我已经在故事板中设计了单元布局。

我已经在这个问题上挣扎了很长一段时间了。任何帮助表示赞赏。

回答

2

当你向上和向下滚动列表时,你看到的错误是否发生?发生这种情况的原因是,您正在连接GetCell方法中的TouchUpInside事件处理程序,并且在重用单元时它永远不会脱钩,因此最终会将多个单元连接到相同的处理程序或多个处理程序!

一般而言,您不想在GetCell方法中对这些类型进行操作,它只能用于DequeueReusableCell或创建一个新的类型。一切都应该在自定义单元格内完成。

看你的代码示例,它看起来并不像你使用的是自定义单元格,所以你可以做的是这样的:

1)在GetCell设置按钮的标签为行在IndexPath

editGoalButton.Tag = indexPath.Row; 

2)在ProgramMeasurementDetailsTableViewSource为事件处理程序创建一个单独的方法:

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的Un钩你rehook的TouchUpInside处理程序之前:

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

虽然这将确保该按钮才迷上了一次,它真的不是来处理这样的情况的正确方法。您应该使用自定义单元格并执行单元格中的所有布线,然后在UITableViewCell中的PrepareForReuse替换中将其解除并清除。我强烈建议去这个Xamarin tutorial

如果你真的经过它...注意如何巩固GetCell方法是。

--UPDATE-- 在你CustomCell类:

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

你只想通过ToggleRow方法操作参数,当你在GetCell调用Setup

+0

所以我不应该使用故事板来设计细胞?现在我已经在故事板中设计了单元格。是否有可能将它用作CustomCell类中的单元设计? – HeisenBerg

+0

您也可以使用Storyboard中的单元格。只要确保“TableView Cell Style”被设置为“Custom”并给它一个类名即可。 Xamarin将为自定义类生成'.cs'文件。本教程的开始部分将向您展示如何生成文件后面的代码:https://developer.xamarin.com/guides/ios/user_interface/tables/autosizing-row-height/ – pnavk

+0

如何在何时定义按钮TouchUpinside使用自定义单元类?我是否会放入自定义单元格类?如果是的话,我如何从那里重新加载tableview行? – HeisenBerg