我的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是不工作的。我把断点,似乎这部分从来没有得到执行。
我已经在故事板中设计了单元布局。
我已经在这个问题上挣扎了很长一段时间了。任何帮助表示赞赏。
所以我不应该使用故事板来设计细胞?现在我已经在故事板中设计了单元格。是否有可能将它用作CustomCell类中的单元设计? – HeisenBerg
您也可以使用Storyboard中的单元格。只要确保“TableView Cell Style”被设置为“Custom”并给它一个类名即可。 Xamarin将为自定义类生成'.cs'文件。本教程的开始部分将向您展示如何生成文件后面的代码:https://developer.xamarin.com/guides/ios/user_interface/tables/autosizing-row-height/ – pnavk
如何在何时定义按钮TouchUpinside使用自定义单元类?我是否会放入自定义单元格类?如果是的话,我如何从那里重新加载tableview行? – HeisenBerg