【问题标题】:Adding a ViewCell to a TableView but the ViewCell height cannot be set?将 ViewCell 添加到 TableView 但无法设置 ViewCell 高度?
【发布时间】:2018-02-01 23:08:45
【问题描述】:

我有一个 TableView 设置如下:

<ContentPage.Content>
   <TableView x:Name="tableView" Intent="Settings" HasUnevenRows="True">
   </TableView>
</ContentPage.Content>

我在后端代码中添加了TextCells

var newSection = new TableSection("Choose Categories");
        foreach (var category in categories)
        {
            var cell = new CategoryViewCell
            {
                BindingContext = category
            };
            cell.SelectedOrToggled += selectCategory;
            newSection.Add(cell);
        }
        // I want to set the height of TableViewFooter to 200 but 
        // it does not happen. I just see a row colored red that 
        // is the same height as all the other rows.
        var cmt = new TableViewFooter(
            "Select a Category and all cards from that category will be added to the deck",
            200);
        newSection.Add(cmt);
        return newSection;

然后我添加这个 ViewCell 并在 C# 支持代码中添加文本并设置高度。

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="Japanese.TableViewFooter">
    <Grid 
        BackgroundColor="Red"
        x:Name="_containerGrid"
        VerticalOptions="CenterAndExpand" 
        Padding="20,10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label 
            x:Name="_commentLbl"
            Style="{DynamicResource ListItemDetailTextStyleStyle}" 
            TextColor="#59595F" 
            HorizontalOptions="FillAndExpand" 
            VerticalOptions="CenterAndExpand" />
    </Grid>
</ViewCell>

public partial class TableViewFooter : ViewCell
{
    public TableViewFooter(string text, double height)
    {
        InitializeComponent();
        _commentLbl.Text = text;
        _commentLbl.HeightRequest = height;
        _containerGrid.HeightRequest = height;
    }
}

但是,无论我做什么,ViewCell(TableViewFooter 类型)的高度仍然与其他行的高度相同。

我尝试设置 HasUnevenRows="True" 但这似乎没有效果。

是否可以更改我添加的最后一个 ViewCell(类型 TableViewFooter)的高度?

【问题讨论】:

  • 我想你需要一个渲染器来调整高度,也许看看这里:forums.xamarin.com/discussion/2889/…
  • 执行 section.Add(new CustomViewCell { Height = 100 });不工作?
  • 我正在尝试将高度设置为 200(第二个参数)但不起作用。

标签: xamarin xamarin.forms


【解决方案1】:

试试这个解决方法:

public TableViewFooter(string text, double height)
{
    InitializeComponent();
    _commentLbl.Text = text;
    Height = height;
}

并在 ViewCell 的 XMAL 中的 Grid 中删除这一行:

VerticalOptions="CenterAndExpand" 

【讨论】:

  • 嗨 Cole,我有一个问题是 TableViewFooter 的高度。
  • @能否附上 TableViewFooter 初始构造函数的代码。
  • 您好,已附上。
  • 我试过你的建议,但还是一样。评论区的高度没有变化,仍然和 TableView 中的所有其他行一样。
  • @SamanthaJTStar 如果可能的话,你能否提供一个简单的演示,它在我身边很好用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多