【问题标题】:Xamarin iOS ViewCellRendererXamarin iOS ViewCellRenderer
【发布时间】:2017-10-19 13:19:10
【问题描述】:

我尝试自定义UITableViewCell。我按照这里的解释做了:Xamarin Customizing a ViewCell

到目前为止一切顺利。 现在我尝试在 xib 中实现单元格的 ui。现在几乎可以工作了。我只有两个问题:

  1. 单元格的行高保持在 44。
  2. 如何访问标签等 ui 元素来更改其文本?

这是我的代码:

ViewCellRenderer:

public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
  var NativeViewCell = (NativeViewCell)item;

  cell = reusableCell as NativeiOSCell;
  if (cell == null)
    cell = new NativeiOSCell(item.GetType().FullName, NativeViewCell);
  else
    cell.NativeViewCell.PropertyChanged -= OnNativeViewCellPropertyChanged;

  NativeViewCell.PropertyChanged += OnNativeViewCellPropertyChanged;
  cell.UpdateCell(NativeViewCell);
  tv.RowHeight = 102;
  return cell;
}

NativeiOSCell:

public NativeiOSCell(string cellId, NativeViewCell cell) : base(UITableViewCellStyle.Default, cellId)
{
  NativeViewCell = cell;
  UITableViewCell view = (UITableViewCell) UINib.FromName("Layout1", NSBundle.MainBundle).Instantiate(null, null)[0];
  this.Bounds = view.Bounds;
  ContentView.AddSubviews(view.ContentView.Subviews);
}

【问题讨论】:

  • 好的,我的第二个问题完成了。它们当然在子视图中,因此我可以从那里访问它们。

标签: c# uitableview xamarin xamarin.ios xamarin.forms


【解决方案1】:

由于 M.D. 刚刚创建了 ViewCellRenderer 而不是 ListViewRenderer ,我们无法访问 GetHeightForRow 或 EstimatedHeight 方法。

方案一:在ListView上设置RowHeight

<ListView x:Name="listView" RowHeight="100">

方案2:将HasUnevenRows设置为True,并在ViewCell上设置Height

<ListView x:Name="listView" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <local:NativeCell  Height="100" Name="{Binding Name}" Category="{Binding Category}" ImageFilename="{Binding ImageFilename}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【讨论】:

    【解决方案2】:

    检查表格的表格源,因为问题与您的自定义单元格无关,问题在于表格源中将该值分配为默认值的方法

    public override nfloat EstimatedHeight(UITableView tableView, NSIndexPath indexPath)
        {
            return base.EstimatedHeight(tableView, indexPath);
        }
    

    试着放一些类似

    的东西
    public override nfloat EstimatedHeight(UITableView tableView, NSIndexPath indexPath)
        {
            return 104;
        }
    

    并检查表格视图的此值不是自动的,并输入您的自定义单元格的值

    我希望这有助于让我知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 2015-10-31
      相关资源
      最近更新 更多