Monotouch.Dialog:带有字母导航的分段UITableViews

时间:2011-03-11 04:06:43

标签: uitableview xamarin.ios monotouch.dialog

我可以使用带字母导航的MonoTouch.Dialog创建一个Sectioned UITableView吗?

在MonoTouch中,我创建了如此划分的UITableViews:

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做同样的事情。这可能吗?

2 个答案:

答案 0 :(得分:4)

我相信如果UITableView的样式很简单,你只能得到索引。

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

答案 1 :(得分:1)

根据Miguel的回答,这就是我所做的:

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;
    }
}