自定义列表视图组短名称样式

时间:2017-08-16 06:42:46

标签: xamarin xamarin.ios

如何在列表视图的右侧自定义分组的短名称?有没有人对列表视图自定义渲染器有所了解?

遵循预期输出

enter image description here

2 个答案:

答案 0 :(得分:0)

官方文档here,请参阅添加索引

示例here,请参阅 BasicTableIndex

<强>步骤

  • 通过dataSource创建短名称Arrays。

    indexedTableItems = new Dictionary<string, List<string>>();
    foreach (var t in items) {
        if (indexedTableItems.ContainsKey (t[0].ToString ())) {
            indexedTableItems[t[0].ToString ()].Add(t);
        } else {
            indexedTableItems.Add (t[0].ToString (), new List<string>() {t});
        }
    }
    keys = indexedTableItems.Keys.ToArray ();
    
  • 通过覆盖这些方法来显示各个部分

    public override nint NumberOfSections (UITableView tableView)
    {
    return keys.Length;
    }
    public override nint RowsInSection (UITableView tableview, nint section)
    {
       return indexedTableItems[keys[section]].Count;
    }
    public override string[] SectionIndexTitles (UITableView tableView)
    {
       return keys;
    }
    

答案 1 :(得分:0)

无需为该

创建渲染

请尝试以下代码模式

您的xaml代码看起来像

<ListView x:Name ="lstView" IsGroupingEnabled="true" GroupDisplayBinding="{Binding LongName}" GroupShortNameBinding="{Binding ShortName}">
    <ListView.ItemTemplate>
        <DataTemplate><TextCell Text="{Binding Name}" Detail = "{Binding Comment}" /></DataTemplate>
    </ListView.ItemTemplate>
</ListView>

您的xaml.cs页面看起来像

private ObservableCollection<GroupedVeggieModel> grouped { get; set; }
        public GroupedListXaml ()
        {
            InitializeComponent ();
            grouped = new ObservableCollection<GroupedVeggieModel> ();
            var veggieGroup = new GroupedVeggieModel () { LongName = "vegetables", ShortName="v" };
            var fruitGroup = new GroupedVeggieModel () { LongName = "fruit", ShortName = "f" };
            veggieGroup.Add (new VeggieModel () { Name = "celery", IsReallyAVeggie = true, Comment = "try ants on a log" });
            veggieGroup.Add (new VeggieModel () { Name = "tomato", IsReallyAVeggie = false, Comment = "pairs well with basil" });
            veggieGroup.Add (new VeggieModel () { Name = "zucchini", IsReallyAVeggie = true, Comment = "zucchini bread > bannana bread" });
            veggieGroup.Add (new VeggieModel () { Name = "peas", IsReallyAVeggie = true, Comment = "like peas in a pod" });
            fruitGroup.Add (new VeggieModel () {Name = "banana", IsReallyAVeggie = false,Comment = "available in chip form factor"});
            fruitGroup.Add (new VeggieModel () {Name = "strawberry", IsReallyAVeggie = false,Comment = "spring plant"});
            fruitGroup.Add (new VeggieModel () {Name = "cherry", IsReallyAVeggie = false,Comment = "topper for icecream"});
            grouped.Add (veggieGroup); grouped.Add (fruitGroup);
            lstView.ItemsSource = grouped;
        }

您的模型看起来像

public class VeggieModel
    {
        public string Name { get; set; }
        public string Comment { get; set; }
        public bool IsReallyAVeggie { get; set; }
        public string Image { get; set; }
        public VeggieModel ()
        {
        }
    }

    public class GroupedVeggieModel : ObservableCollection<VeggieModel>
    {
        public string LongName { get; set; }
        public string ShortName { get; set; }
    }

源代码here