我有一个字符串列表,需要在 UITableView
中显示。它们必须有不同的布局
,所以我制作了 4 个不同的 TableViewCells
。在 GetCell
中,我检查项目的类型(例如,列表项是“h_Head
”)并返回相应的单元格。有些条目很长,占据两行。问题是它们被剪掉了。当它最初加载时,一切都很好。当我滚动经过多行字符串并向上滚动时,它们会被切断。 Initial load After scrolling past it and back up
这是我的代码:
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath) {
BaseChecklistTableViewCell cell;
var item = dict [indexPath.Row];
string[] data = item.Split ('_');
var type = data [0];
var content = data [1];
cell = tableView.DequeueReusableCell (new NSString ("NormalTableViewCell")) as BaseChecklistTableViewCell;
//indented cell
if (type.Equals ("i") && (cell == null || !cell.Key.Equals ("IndentTableViewCell"))) {
cell = IndentTableViewCell.Create ();
//normal cell
} else if (type.Equals ("n") && (cell == null || !cell.Key.Equals ("NormalTableViewCell"))) {
cell = NormalTableViewCell.Create ();
//header cell
} else if (type.Equals ("h") && (cell == null || !cell.Key.Equals ("HeadTableViewCell"))) {
cell = HeadTableViewCell.Create ();
//title cell
} else if (cell == null || !cell.Key.Equals ("TitleTableViewCell")) {
cell = TitleTableViewCell.Create ();
}
cell.setData (content);
return cell;
}
public override nfloat GetHeightForRow (UITableView tableView, NSIndexPath indexPath) {
string item = dict [indexPath.Row];
SizeF size = new SizeF ((float)tableView.Bounds.Width - 40, float.MaxValue);
nfloat height = UIStringDrawing.StringSize (item, UIFont.FromName("DINPro-Regular", 15), size, UILineBreakMode.WordWrap).Height + 20;
return height;
}
最佳答案
您的 UITableView
的标准高度为 44px,这就是向上滚动时显示的高度。
您可以尝试设置:
TableView.RowHeight = UITableView.AutomaticDimension;
此方法起作用的条件是您正在使用约束。
这应该使您的 TableCell
的高度通用。无需每次都设置高度属性。
关于c# - 滚动时自定义 TableViewCell 的高度被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36493882/