Monotouch.Dialog: секционные UITableViews с алфавитной навигацией - PullRequest
2 голосов
/ 11 марта 2011

Могу ли я создать секционированный UITableView с MonoTouch.Dialog с алфавитной навигацией?

В 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 ]

4 голосов
/ 11 марта 2011

Я считаю, что вы можете получить индекс только в том случае, если стиль UITableView прост.

Кроме того, вам необходимо переопределить метод SectionIndexTitles для UITableViewDataSource, поскольку MonoTouch.Dialog использует свою собственную реализацию в классах SizingSource или Source, что вам нужно сделать, это создать эти два подкласса для возврата значений, а затем перехватить перезаписать метод DialogViewController.CreateSizingSource ().

1 голос
/ 11 марта 2011

На основании ответа Мигеля вот что я сделал:

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