简体   繁体   中英

Xamarin Forms - iOS dynamic ViewCell size in a ListView

The following XF app (code below) creates a simple ListView with 2 custom cells. Tapping on a cell uses the IsVisible property to show the second label.

On Android this works great as the size of the ViewCell resizes to fit the currently displayed contents. When the Detail item is made visible, the ViewCell expands to show the detail.

On iOS, this isn't working.

Here is how the app appears on first launch...

在此处输入图片说明

When you tap the first ViewCell, the IsVisible property is tripped and the Detail item shows. However, the ViewCell remains the same height causing it to overflow as seen below...

在此处输入图片说明

How can this be accomplished on the iOS side?

Here is the code...

XAML

  <ContentPage.Content>
    <ListView x:Name="___list" Margin="50" HasUnevenRows="True">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout>
              <StackLayout.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding CellTap}" />
              </StackLayout.GestureRecognizers>
              <Label Text="{Binding Title}" />
              <Label Text="{Binding Detail}" FontSize="30" IsVisible="{Binding ShowDetails}" />
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </ContentPage.Content>

C#

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        ___list.ItemsSource = new List<Element>() {
                new Element() {
                    Title="First Element",
                    Detail = "First Element Details"
                },
                new Element() {
                    Title="Second Element",
                    Detail = "Second Element Details"
                }
            };
    }
}
public class Element : INotifyPropertyChanged
{
    public Element()
    {
        CellTap = new Command(() =>
        {
            ShowDetails = !ShowDetails;
        });
    }

    public ICommand CellTap { get; private set; }

    private string _title;
    public string Title
    {
        get { return _title; }
        set { if (_title != value) { _title = value; OnPropertyChanged("Title"); } }
    }
    private string _detail;
    public string Detail
    {
        get { return _detail; }
        set { if (_detail != value) { _detail = value; OnPropertyChanged("Detail"); } }
    }
    private bool _showDetails;
    public bool ShowDetails
    {
        get { return _showDetails; }
        set { if (_showDetails != value) { _showDetails = value; OnPropertyChanged("ShowDetails"); } }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

ViewCell cannot automatically find out how high it is supposed to be. You'll have to support it by setting its Height or force it to update. Unfortunately, Height is not bindable.

Option 1: use this if you have different heights per row and the list cannot figure out the correct height.

class CustomViewCell : ViewCell
{
  protected override void OnBindingContextChanged()
  {
    base.OnBindingContextChanged();
    // Do some calculation in here to get the height you need.
    // Here we are using an example that bases the size on the result of ToString()
    string text = BindingContext.ToString();
    Height = 10 + ((int)(text[0]) - 65);
  } 
}

Option 2: change height dynamically (probably what you want)

void SomeEventHandler(object sender, EventArgs args)
{
   // Let's assume an image was tapped...
   var image = sender as Image;
   // ...and the image is in a cell.
   var viewCell = image.Parent.Parent as ViewCell;

   // You would FIRST change the height of the content (in this case the image)
   if (image.HeightRequest < 250)
   {
       image.HeightRequest = image.Height + 100;
       // And THEN tell the cell to update (Note: you should not be required
       // to subclass the cell)
       viewCell.ForceUpdateSize();
   }
}

Make sure that HasUnevenRows = true , otherwise forcing the update won't have an effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM