Creating UITable with section using Mono touch and slodge mvvmcross

后端 未结 1 390
暗喜
暗喜 2021-01-03 08:08

I am trying to create a UItable with sections using mono touch and slodge mvvmcross. However I am having some issues.

In my data I have a range of sections with 1-2

相关标签:
1条回答
  • 2021-01-03 08:31

    I don't really understand the problem you are seeing at present.

    However, I can explain how I used sections in BaseSessionListView.cs the conference sample.

    Basically, in that sample the ItemsSource was a grouped source - so the ViewModel code was:

    public class SessionGroup : List<Session>
    {
        public string Key { get; set; }
    
        public SessionGroup(string key, IEnumerable<Session> items)
            : base(sessions)
        {
            Key = key;
        }
    }
    
    private List<SessionGroup> _groupedList;
    public List<SessionGroup> GroupedList
    {
        get { return _groupedList; }
        protected set { _groupedList = value; RaisePropertyChanged("GroupedList"); }
    }
    

    This meant that my exposed ItemsSource had a structure a bit like:

    Group
        Session
        Session
        Session
    Group
        Session
        Session
        Session
        Session
    Group
        Session
        Session
    etc
    

    The Section based methods in the table source were then:

    public override string[] SectionIndexTitles(UITableView tableView)
    {
      if (_sessionGroups == null)
           return base.SectionIndexTitles(tableView);
    
       return _sessionGroups.Select(x => KeyToString(x.Key, 10)).ToArray();
    }
    
    protected override object GetItemAt(NSIndexPath indexPath)
    {
       if (_sessionGroups == null)
           return null;
    
       return _sessionGroups[indexPath.Section][indexPath.Row];
    }
    
    public override int NumberOfSections(UITableView tableView)
    {
        if (_sessionGroups == null)
            return 0;
    
        return _sessionGroups.Count;
    }
    
    public override int RowsInSection(UITableView tableview, int section)
    {
        if (_sessionGroups == null)
            return 0;
    
        return _sessionGroups[section].Count;
    }
    

    This seemed to work.... but I don't know if it helps with your question?


    If I'd wanted to add a header element, then I guess I would have just changed the RowsInSection and GetItemAt methods to accomodate this - plus the GetOrCreateCellFor to return header element cells too... but I guess there will be other ways to do this too?

    0 讨论(0)
提交回复
热议问题