Monotouch.Dialog:带字母导航的分区UITableView

Monotouch.Dialog:带字母导航的分区UITableView,uitableview,xamarin.ios,monotouch.dialog,Uitableview,Xamarin.ios,Monotouch.dialog,我是否可以使用MonoTouch创建分区UITableView。带有字母导航的对话框 在MonoTouch中,我创建了分区UITableView,如下所示: public EntityDataSource(UIViewController controller) { _controller = controller; this._entities = repository.GetEntities(); sectionTitles = (from r in _entiti

我是否可以使用MonoTouch创建分区UITableView。带有字母导航的对话框

在MonoTouch中,我创建了分区UITableView,如下所示:

public EntityDataSource(UIViewController controller)
{
    _controller = controller;
    this._entities = repository.GetEntities();

    sectionTitles = (from r in _entities
        orderby r.StartsWith
        select r.StartsWith).Distinct().ToList();

    foreach (var entity in _entities)
    {   
        int sectionNumber = sectionTitles.IndexOf(entity.StartsWith);
        if (sectionElements.ContainsKey(sectionNumber)) {
        sectionElements[sectionNumber].Add(entity);
        }
        else {
        sectionElements.Add(sectionNumber, new List<Entity>() {entity});
        }
    }
}

public override int NumberOfSections (UITableView tableView)
{
    return sectionTitles.Count;
}

public override string TitleForHeader (UITableView tableView, int section)
{
    return sectionTitles[section];
}

public override string[] SectionIndexTitles (UITableView tableView)
{
   return sectionTitles.ToArray();
}

public override int RowsInSection (UITableView tableview, int section)
{
    return sectionElements[section].Count(); 
}
公共实体数据源(UIViewController) { _控制器=控制器; 这是。_entities=repository.GetEntities(); sectionTitles=(来自r in_实体) orderby r.StartsWith 选择r.StartsWith).Distinct().ToList(); foreach(var实体在_实体中) { int sectionNumber=sectionTitles.IndexOf(entity.StartsWith); if(sectionElements.ContainsKey(sectionNumber)){ sectionElements[sectionNumber]。添加(实体); } 否则{ 添加(sectionNumber,new List(){entity}); } } } 公共覆盖int NumberOfSections(UITableView表格视图) { 返回标题。计数; } 公共重写字符串标题ForHeader(UITableView tableView,int部分) { 返回章节标题[章节]; } 公共重写字符串[]节索引标题(UITableView tableView) { 返回sectionTitles.ToArray(); } 公共覆盖int RowsInSection(UITableView表格视图,int section) { 返回sectionElements[section].Count(); }
我想用MonoTouch.Dialog做同样的事情。这可能吗?

根据米格尔的回答,以下是我所做的:

public class EntityViewController : DialogViewController {
    DialogViewController parent;        
    List<string> sectionTitles;     

    class EntitySource : Source {
        EntityViewController parent;

        public EntitySource (EntityViewController parent) : base (parent)
        {
            this.parent = parent;
        }

        public override string[] SectionIndexTitles (UITableView tableView)
        {
            return parent.sectionTitles.ToArray();
        }
    }

    class SizingIndexedSource : Source {
        EntityViewController parent;

        public SizingIndexedSource (EntityViewController parent) : base (parent)
        {
            this.parent = parent;
        }

        public override string[] SectionIndexTitles (UITableView tableView)
        {
            return parent.sectionTitles.ToArray();
        }
    }

    public override Source CreateSizingSource (bool unevenRows)
    {
        if (unevenRows)
            return new SizingIndexedSource (this);
        else
            return new EntitySource (this);;
    }

    private RootElement GetEntities() {
        EntityRepository db = new EntityRepository();
        List<Entity> _entities = db.GetEntities();
        sectionTitles = (from r in _entities
                        orderby r.StartsWith
                        select r.StartsWith).Distinct().ToList();
        var root = new RootElement ("Entities") ;
        foreach (var item in sectionTitles) {               
            var section = new Section(item,String.Empty);
            foreach (var entity in _entities.Where(e => e.StartsWith == item)) { 
                section.Add(new StringElement(entity.FirstName + " " + entity.LastName, "Title"));
            }
            root.Add(section);
        }
        return root;
    }

    public EntityViewController (DialogViewController parent) : base (UITableViewStyle.Grouped, null)
    {
        this.parent = parent;           
        Root = GetEntities();           
        this.Style = UITableViewStyle.Plain;
        this.EnableSearch = true;
        this.SearchPlaceholder = "Find a contact";
        this.AutoHideSearch = true;
    }
}
公共类EntityViewController:DialogViewController{ DialogViewController父级; 列出部门名称; 类EntitySource:源{ EntityViewController父级; 公共EntitySource(EntityViewController父级):基本(父级) { this.parent=parent; } 公共重写字符串[]节索引标题(UITableView tableView) { 返回parent.sectionTitles.ToArray(); } } 类SizingIndexedSource:源{ EntityViewController父级; 公共大小索引源(EntityViewController父级):基本(父级) { this.parent=parent; } 公共重写字符串[]节索引标题(UITableView tableView) { 返回parent.sectionTitles.ToArray(); } } 公共覆盖源CreateSizengSource(布尔行) { 如果(行) 返回新的SizingIndexedSource(此); 其他的 返回新的EntitySource(this);; } 私有根元素GetEntities(){ EntityRepository db=新EntityRepository(); List_entities=db.GetEntities(); sectionTitles=(来自r in_实体) orderby r.StartsWith 选择r.StartsWith).Distinct().ToList(); var root=新的根元素(“实体”); foreach(节标题中的变量项){ var节=新节(项,字符串.Empty); foreach(在_entities.Where(e=>e.StartsWith==item)中的var实体){ section.Add(新的StringElement(entity.FirstName+“”+entity.LastName,“Title”); } 根。添加(节); } 返回根; } public EntityViewController(DialogViewController父级):基础(UITableViewStyle.Grouped,null) { this.parent=parent; Root=GetEntities(); this.Style=UITableViewStyle.Plain; this.EnableSearch=true; this.SearchPlaceholder=“查找联系人”; this.AutoHideSearch=true; } }
我相信只有在UITableView的样式是普通的情况下才能获得索引


此外,您需要重写UITableViewDataSource上的方法SectionIndexTitles,因为MonoTouch.Dialog在类SizingSource或Source中使用自己的实现,您需要做的是创建这两个子类以返回值,然后通过重写DialogViewController.CreateSizingSource()将它们连接起来方法。

谢谢,我来试一试!只是一个小注释,您还应该为SizingSource创建一个子类,并基于该子类创建EntitySource或EntitySizingSource(因此,您支持不同大小的元素。我已将此示例添加到MonoTouch.Dialog中,希望它能帮助其他人。尽管出于某种原因,我没有在右侧显示标题,即使搜索栏功能正常。听起来不错,我将添加该子类。re:您的第二条评论。当您说标题时,您是指alpha吗右侧的导航栏,或应在右侧呈现的StringElement的第二个参数的值?