2

我可以使用带有字母导航的 MonoTouch.Dialog 创建分段 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(); 
}

我想用 MonoTouch.Dialog 做同样的事情。那可能吗?

4

2 回答 2

4

我相信只有 UITableView 的样式是普通的,你才能得到索引。

另外,你需要重写 UITableViewDataSource 上的 SectionIndexTitles 方法,因为 MonoTouch.Dialog 在 SizingSource 或 Source 类中使用了它自己的实现,你要做的就是创建这两个子类来返回值,然后通过覆盖 DialogViewController.CreateSizingSource () 方法。

于 2011-03-11T05:41:20.083 回答
1

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

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;
    }
}
于 2011-03-11T05:11:44.780 回答