c# - 滚动时自定义 TableViewCell 的高度被切断

标签 c# ios uitableview xamarin xamarin.ios

我有一个字符串列表,需要在 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/

相关文章:

c# - 在大型 html 文档中为图像添加缺少的 alt 标签的最有效方法

c# - 将 char*[] 从 c++ dll 结构传递到 c#

ios - 如何将 iPhone OSStatus 代码转换为有用的代码?

ios - 带有部分的快速表格 View 无法使用搜索栏

ios - 如何在自定义单元格中添加按钮?

ios - 调用 reloadData 后 UITableView 动态单元格高度滚动到顶部

c# - 如何将 Visual Studio 2010 Express C# 连接到 SQL Server Express

c# - TreeView 仅在复选框处忽略双击

ios - 如何在屏幕之间执行清晰的导航?

ios - 如何通过平移手势旋转 SCNSphere/Node