开发者

Monotouch.Dialog : Sectioned UITableViews with alphabetic navigation

Can I create a Sectioned UITableView with MonoTouch.Dialog with alphabetic navigation?

In MonoTouch, I create sectioned UITableViews like so:

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

I want to do this same thing but 开发者_开发知识库with MonoTouch.Dialog. Is that possible?


I believe you can only get the index if the style of the UITableView is plain.

In addition, you need to override the method SectionIndexTitles on the UITableViewDataSource, since MonoTouch.Dialog uses its own implementation in the classes SizingSource or Source, what you have to do is create those two subclasses to return the values, and then hook them up by overwriting the DialogViewController.CreateSizingSource () method.


Based on Miguel's answer, here is what I did:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜