Создание UITable с разделом с использованием Mono Touch и Slodge mvvmcross

Я пытаюсь создать пользовательский интерфейс с разделами, используя моно touch и slodge mvvmcross. Однако у меня есть некоторые проблемы.

В моих данных у меня есть ряд разделов с 1-2 элементами, и у моего источника таблицы есть список (ItemsSource), содержащий все элементы.

Он показывает все разделы правильно, однако кажется, что для каждого раздела он берет элемент 0 и / или 1 из общего списка элементов и не учитывает, что это новый раздел, если это новый раздел индекса должен иметь смещение. Но я могу ошибаться.

Должен ли я иметь несколько списков с элементами и с таблицами source.Itemsource в соответствии с разделом - я попробовал этот подход, однако он пошел по спирали гибели: P

Любая помощь приветствуется :)

Вот мой код для просмотра таблицы:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using Cirrious.MvvmCross.Binding.Touch.ExtensionMethods;
using Cirrious.MvvmCross.Binding.Touch.Views;
using Cirrious.MvvmCross.Commands;
using Cirrious.MvvmCross.Interfaces.Commands;
using Cirrious.MvvmCross.Views;
using CmsApp.Core.Interfaces;
using CmsApp.Core.Model;
using CmsApp.Core.ViewModels;
using CmsApp.Core.ViewModels.Items;
using CmsApp.GtilP.Core.ViewModels.Items;
using CmsApp.Touch.Views.Items;
using MonoTouch.CoreGraphics;
using MonoTouch.ObjCRuntime;

namespace CmsApp.Touch

{
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;

    [Register("WeekItemListViewController")] 
    public class WeekItemListViewController:CustomListViewController
    {

        public WeekItemListViewController(IntPtr handle): base(handle)
            {

            }

    private bool _hide;


    private List _cells;
    private UmbracoTableViewController _table;
    private TabelViewSource _tableSource;
    private List _listOfTasks;
    private List _listOfHeaders;
    private List _listOfFooter;
    private List _listOfGroupedTasks;

    public bool Hide
    {
        get { return _hide; }
        set { _hide = value;
            this.Hidden = _hide;
        }
    }

    public BaseViewModel ViewModel { get; set; }

    //custom implementation for adding views to the button row

    protected override void AddViews()
    {

        SortItemsAcoordingToTyoe(ItemsSource);

        if(ItemsSource.Count==0)
        {
            this.Hidden = true;
            return;
        }
        else
        {
            this.Hidden = false;
        }

        if(_table!=null)
        {
            _table.View.RemoveFromSuperview();
        }

            _table = new UmbracoTableViewController(new RectangleF(0,0,this.Frame.Width,this.Frame.Height), UITableViewStyle.Plain);

            _table.TableView.BackgroundColor=UIColor.Clear;
            _tableSource = new TabelViewSource(_table.TableView,_listOfHeaders,_listOfFooter,_listOfGroupedTasks);

        _tableSource.SelectionChanged += (sender, args) => DoTableSelect((TableSectionItemViewModel)args.AddedItems[0]);

        _tableSource.ItemsSource=InitTableCells();;

            _table.TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
            _table.TableView.Source = _tableSource;

        this.AddSubview(_table.View);


    }

    private void SortItemsAcoordingToTyoe(IList itemsSource)
    {
         _listOfGroupedTasks = new List();
         _listOfTasks =new List();
         _listOfHeaders=new List();
         _listOfFooter = new List();

        foreach (SectionItemBaseViewModel sectionItemBaseViewModel in itemsSource)
        {

            if(sectionItemBaseViewModel.GetType()==typeof(WeekItemHeaderViewModel))
            {

                _listOfHeaders.Add((WeekItemHeaderViewModel)sectionItemBaseViewModel);
                _listOfTasks=new List();
                _listOfGroupedTasks.Add(_listOfTasks);
            }
            else if (sectionItemBaseViewModel.GetType() == typeof(WeekItemTaskViewModel))
            {
                _listOfTasks.Add((WeekItemTaskViewModel)sectionItemBaseViewModel);
            }
            else if (sectionItemBaseViewModel.GetType() == typeof(WeekItemFooterViewModel))
            {
                _listOfFooter.Add((WeekItemFooterViewModel)sectionItemBaseViewModel);
            }
        }

    }


    private List InitTableCells()
    {
        _cells = new List();


        foreach (List listOfGroupedTask in _listOfGroupedTasks)
        {
            foreach (WeekItemTaskViewModel item in listOfGroupedTask)
            {
                var tableCell = new TableSectionItemViewModel() { TaskViewModel = item };
                _cells.Add(tableCell);
            }

        }


        return _cells;
    }



    private void DoTableSelect(TableSectionItemViewModel tableItemViewModel)
    {
        /*
        string selected = tableItemViewModel.Title;
        ((WelcomeScreenViewModel) ViewModel).SortViews(selected);

        _titleLabel.Text = selected;

        */
        int section=0;
        int row=0;
        NSIndexPath index=null;
        foreach(var group in _listOfGroupedTasks)
        {

            if(group.Contains(tableItemViewModel.TaskViewModel)){

                row=group.IndexOf(tableItemViewModel.TaskViewModel);
        index = NSIndexPath.FromRowSection(row, section);
                break;
            }
            section++;
        }

        if(index!=null){
            _table.TableView.DeselectRow(index, false);}
    }

    private class TabelViewSource : MvxBindableTableViewSource
    {
        private List _listOfHeaders;
        private List _listOfFooter;
        private List _listOfGroupedTasks;

        public TabelViewSource(UITableView view, List listOfHeaders, List listOfFooter, List listOfGroupedTasks)
            : base(view)
        {

            _listOfFooter = listOfFooter;
            _listOfGroupedTasks = listOfGroupedTasks;
            _listOfHeaders = listOfHeaders;

        }
        public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return 50.0f;
        }

        public override int NumberOfSections(UITableView tableview)
        {
            return _listOfHeaders.Count;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            List list=_listOfGroupedTasks[section];

            //this.ItemsSource=list;
            return list.Count;
        }

        public override int SectionFor(UITableView tableview, string title, int atIndex)
        {
            return atIndex;
        }



        protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
        {
            //var reuse = tableView.DequeueReusableCell(TaskTableCell.Identifier);

     //       var listOfTasks = _listOfGroupedTasks[indexPath.Section];

       //     var task = listOfTasks[indexPath.Row];

         //   if (reuse != null)
           // {
             //   return reuse;
            //}
            // tjek på type - sektion, item footer
            var toReturn = TaskTableCell.LoadFromNib();

            return toReturn;
        }

/*
        public override UIView GetViewForFooter(UITableView tableView, int section)
        {
            return base.GetViewForFooter(tableView, section);
        }
*/
        public override string TitleForHeader(UITableView tableView, int section)
        {
            WeekItemHeaderViewModel header = _listOfHeaders[section];
            return header.Title;
        }
    }

}

public class TableSectionItemViewModel 
{
    public WeekItemTaskViewModel TaskViewModel { get; set; }

    public string Title
    {
        get{return TaskViewModel.TaskName;}

    }

    public bool IsExpired
    {
        get{
            return TaskViewModel.IsExpired;
        }
    }

    public bool TaskIsDone
    {
        get{
            return TaskViewModel.TaskIsDone;
        }
    }


    public IImageItem CellBackground
    {
        get
        {
            return new LocalImageItem("tablecell_background.png");
        }
    }

    public string[] CheckMarkImage
    {
        get
        {
            return TaskViewModel.CheckMarkImageData;
        }
    }

    public IMvxCommand ToggleDone
    {
        get { return new MvxRelayCommand(CheckBoxPushed); }
    }

    private void CheckBoxPushed()
    {
        TaskViewModel.TaskIsDone=!TaskIsDone;
    }
    }
}

Ответы на вопрос(1)

Ваш ответ на вопрос