[iOS] Fix NRE when Grouped ListView w/DataTemplateSelector has uneven rows (#1254)

* Add repro for 60524

* [iOS] Get first item instead of first group to measure ListView row height

* Linker strikes again
This commit is contained in:
Samantha Houts 2017-11-08 11:46:33 -08:00 коммит произвёл Samantha Houts
Родитель 8bfb74014c
Коммит 40eadeb644
3 изменённых файлов: 188 добавлений и 5 удалений

Просмотреть файл

@ -0,0 +1,176 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Xamarin.Forms.Internals;
using Xamarin.Forms.CustomAttributes;
#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Bugzilla, 60524, "NRE when rendering ListView with grouping enabled and HasUnevenRows set to true", PlatformAffected.iOS)]
public class Bugzilla60524 : TestNavigationPage
{
[Preserve(AllMembers = true)]
public class GroupedItemsPage : ContentPage
{
private ObservableCollection<Grouping<string, GroupedItem>> model;
private ListView listView;
public GroupedItemsPage()
{
listView = new ListView(ListViewCachingStrategy.RetainElement) { HasUnevenRows = true };
listView.IsGroupingEnabled = true;
listView.ItemTemplate = new GroupedItemsDataTemplateSelector();
var headerCell = new DataTemplate(typeof(TextCell));
headerCell.SetBinding(TextCell.TextProperty, "Key");
this.listView.GroupHeaderTemplate = headerCell;
this.model = new ObservableCollection<Grouping<string, GroupedItem>>();
this.GetItems();
this.SetMainContent();
}
private void GetItems()
{
var firstGroup = new Grouping<string, GroupedItem>("Group 1", new List<GroupedItem> {
new GroupedItem("Group 1", "Item 1"),
new GroupedItem("Group 1", "Item 2")
});
var secondGroup = new Grouping<string, GroupedItem>("Group 2", new List<GroupedItem> {
new GroupedItem("Group 2", "Item 3"),
new GroupedItem("Group 2", "Item 4")
});
model.Add(firstGroup);
model.Add(secondGroup);
this.listView.ItemsSource = model;
}
private void SetMainContent()
{
var content = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { new Label { Text = "If this page does not crash, this test has passed." }, this.listView }
};
Content = content;
}
}
[Preserve(AllMembers = true)]
public class GroupedItemsDataTemplateSelector : Xamarin.Forms.DataTemplateSelector
{
private readonly DataTemplate firstGroupTemplate;
private readonly DataTemplate secondGroupTemplate;
public GroupedItemsDataTemplateSelector()
{
// Retain instances
var firstTemplate = new DataTemplate(typeof(TextCell));
firstTemplate.SetBinding(TextCell.TextProperty, "Item");
this.firstGroupTemplate = firstTemplate;
var secondTemplate = new DataTemplate(typeof(ImageCell));
secondTemplate.SetBinding(ImageCell.TextProperty, "Item");
this.secondGroupTemplate = secondTemplate;
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var model = item as GroupedItem;
if (model == null)
{
return null;
}
return model.Group == "Group 1" ? this.firstGroupTemplate : this.secondGroupTemplate;
}
}
[Preserve(AllMembers = true)]
public class GroupedItem
{
public string Group { get; set; }
public string Item { get; set; }
public GroupedItem(string group, string item)
{
this.Group = group;
this.Item = item;
}
}
[Preserve(AllMembers = true)]
public class Grouping<K, T> : ObservableCollection<T>
{
public K Key { get; private set; }
public IList<T> Values
{
get { return this.Items; }
}
public Grouping(K key, IEnumerable<T> items)
{
Key = key;
foreach (var item in items)
{
this.Items.Add(item);
}
}
public Grouping(IGrouping<K, T> grouping)
{
Key = grouping.Key;
foreach (var item in grouping)
{
this.Items.Add(item);
}
}
public void AddRange(IList<T> values)
{
foreach (var item in values)
{
this.Items.Add(item);
}
}
}
[Preserve(AllMembers = true)]
public class GroupingKey
{
public string Title { get; private set; }
public string Abbreviation { get; private set; }
public GroupingKey(string Title, string abbrevation)
{
this.Title = Title;
this.Abbreviation = abbrevation;
}
}
protected override void Init()
{
Navigation.PushAsync(new GroupedItemsPage());
}
#if UITEST
[Test]
public void Bugzilla60524Test()
{
RunningApp.WaitForElement(q => q.Marked("Group 1"));
}
#endif
}
}

Просмотреть файл

@ -330,6 +330,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla58875.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla59896.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla56771.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla60524.cs" />
<Compile Include="$(MSBuildThisFileDirectory)_Template.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla42620.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1028.cs" />
@ -756,4 +757,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
</Project>

Просмотреть файл

@ -358,7 +358,7 @@ namespace Xamarin.Forms.Platform.iOS
{
Control.Layer.RemoveAllAnimations();
//iOS11 hack
if(Forms.IsiOS11OrNewer)
if (Forms.IsiOS11OrNewer)
{
await Task.Delay(1);
}
@ -662,10 +662,16 @@ namespace Xamarin.Forms.Platform.iOS
}
// We're going to base our estimate off of the first cell
var firstCell = templatedItems.ActivateContent(0, templatedItems.ListProxy[0]);
Cell firstCell;
var isGroupingEnabled = List.IsGroupingEnabled;
if (isGroupingEnabled)
firstCell = templatedItems.ActivateContent(0, templatedItems.GetGroup(0)?.ListProxy[0]);
else
firstCell = templatedItems.ActivateContent(0, templatedItems.ListProxy[0]);
// Let's skip this optimization for grouped lists. It will likely cause more trouble than it's worth.
if (firstCell.Height > 0 && !List.IsGroupingEnabled)
if (firstCell?.Height > 0 && !isGroupingEnabled)
{
// Seems like we've got cells which already specify their height; since the heights are known,
// we don't need to use estimatedRowHeight at all; zero will disable it and use the known heights.
@ -882,7 +888,7 @@ namespace Xamarin.Forms.Platform.iOS
PreserveActivityIndicatorState(cell);
return nativeCell;
}
public override nfloat GetHeightForHeader(UITableView tableView, nint section)
{
if (List.IsGroupingEnabled)