Uitableview 使用Mono touch和slodge mvvmcross创建UITable with section

Uitableview 使用Mono touch和slodge mvvmcross创建UITable with section,uitableview,mono,xamarin.ios,mvvmcross,Uitableview,Mono,Xamarin.ios,Mvvmcross,我正在尝试使用mono touch和slodge mvvmcross创建一个包含多个部分的UItable。但是我有一些问题 在我的数据中,我有一系列包含1-2个元素的部分,我的表源有一个包含所有元素的列表(ItemsSource) 它显示的所有部分都是正确的,但是似乎对于每个部分,它都从元素的总列表中获取元素0和/或1,并且不考虑它是一个新的部分,如果它是一个新的部分,索引应该有一个偏移量。但我可能错了 我应该有多个元素列表,并切换表源。Itemsource根据章节-我尝试了这种方法,但它没有进

我正在尝试使用mono touch和slodge mvvmcross创建一个包含多个部分的UItable。但是我有一些问题

在我的数据中,我有一系列包含1-2个元素的部分,我的表源有一个包含所有元素的列表(ItemsSource)

它显示的所有部分都是正确的,但是似乎对于每个部分,它都从元素的总列表中获取元素0和/或1,并且不考虑它是一个新的部分,如果它是一个新的部分,索引应该有一个偏移量。但我可能错了

我应该有多个元素列表,并切换表源。Itemsource根据章节-我尝试了这种方法,但它没有进入厄运螺旋:p

感谢您的帮助:)

以下是我的tableview代码:

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<TableSectionItemViewModel> _cells;
    private UmbracoTableViewController _table;
    private TabelViewSource _tableSource;
    private List<WeekItemTaskViewModel> _listOfTasks;
    private List<WeekItemHeaderViewModel> _listOfHeaders;
    private List<WeekItemFooterViewModel> _listOfFooter;
    private List<List<WeekItemTaskViewModel>> _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<List<WeekItemTaskViewModel>>();
         _listOfTasks =new List<WeekItemTaskViewModel>();
         _listOfHeaders=new List<WeekItemHeaderViewModel>();
         _listOfFooter = new List<WeekItemFooterViewModel>();

        foreach (SectionItemBaseViewModel sectionItemBaseViewModel in itemsSource)
        {

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

                _listOfHeaders.Add((WeekItemHeaderViewModel)sectionItemBaseViewModel);
                _listOfTasks=new List<WeekItemTaskViewModel>();
                _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<TableSectionItemViewModel> InitTableCells()
    {
        _cells = new List<TableSectionItemViewModel>();


        foreach (List<WeekItemTaskViewModel> 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<WeekItemHeaderViewModel> _listOfHeaders;
        private List<WeekItemFooterViewModel> _listOfFooter;
        private List<List<WeekItemTaskViewModel>> _listOfGroupedTasks;

        public TabelViewSource(UITableView view, List<WeekItemHeaderViewModel> listOfHeaders, List<WeekItemFooterViewModel> listOfFooter, List<List<WeekItemTaskViewModel>> 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<WeekItemTaskViewModel> 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;
    }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用系统图;
利用制度全球化;
使用cirries.MvvmCross.Binding.Touch.ExtensionMethods;
使用cirries.MvvmCross.Binding.Touch.Views;
使用cirries.MvvmCross.Commands;
使用cirries.MvvmCross.Interfaces.Commands;
使用cirries.MvvmCross.Views;
使用CmsApp.Core.Interfaces;
使用CmsApp.Core.Model;
使用CmsApp.Core.ViewModels;
使用CmsApp.Core.ViewModels.Items;
使用CmsApp.GtilP.Core.ViewModels.Items;
使用CmsApp.Touch.Views.Items;
使用MonoTouch.CoreGraphics;
使用MonoTouch.objc运行时;
名称空间CmsApp.Touch
{
使用单调的基础;
使用MonoTouch.UIKit;
[注册(“WeekItemListViewController”)]
公共类WeekItemListViewController:CustomListViewController
{
public WeekItemListViewController(IntPtr句柄):基本(句柄)
{
}
私人藏匿处;
私有列表单元;
专用UmbracoTableViewController _表;
私有选项卡elviewsource _tableSource;
私有列表(listoftask);;
私人名单(首长名单);;
私人名单(listofooter);;
私有列表_组任务列表;
公共兽皮
{
获取{return\u hide;}
设置{u hide=value;
this.Hidden=\u hide;
}
}
公共BaseViewModel视图模型{get;set;}
//用于向按钮行添加视图的自定义实现
受保护的覆盖void AddViews()
{
SortItemsCoordingTotyoe(项目来源);
如果(ItemsSource.Count==0)
{
Hidden=true;
返回;
}
其他的
{
这个。隐藏=错误;
}
如果(_table!=null)
{
_table.View.RemoveFromSuperview();
}
_table=新的UmbracoTableViewController(新的矩形F(0,0,this.Frame.Width,this.Frame.Height),UITableViewStyle.Plain);
_table.TableView.BackgroundColor=UIColor.Clear;
_tableSource=新的tableViewSource(_table.TableView,_headers列表,_listOfFooter,_groupedTasks列表);
_tableSource.SelectionChanged+=(发送方,参数)=>DoTableSelect((TableSectionItemViewModel)args.AddedItems[0]);
_tableSource.ItemsSource=InitTableCells();;
_table.TableView.SeparatorStyle=UITableViewCellSeparatorStyle.None;
_table.TableView.Source=\u tableSource;
this.AddSubview(_table.View);
}
私人无效SortItemsCoordingTotyoE(IList itemsSource)
{
_listOfGroupedTasks=新列表();
_listOfTasks=新列表();
_listOfHeaders=新列表();
_listOfFooter=新列表();
foreach(itemsSource中的SectionItemBaseViewModel SectionItemBaseViewModel)
{
if(sectionItemBaseViewModel.GetType()==typeof(WeeKitemHeaderServiceWModel))
{
_添加((WeeKitemHeadServiceWModel)部分ItemBaseViewModel);
_listOfTasks=新列表();
_添加(_listOfTasks);
}
else if(sectionItemBaseViewModel.GetType()==typeof(WeekItemTaskViewModel))
{
_添加((WeekItemTaskViewModel)部分ItemBaseViewModel);
}
else if(sectionItemBaseViewModel.GetType()==typeof(WeekItemFooterViewModel))
{
_添加((WeekItemFooterViewModel)部分ItemBaseViewModel);
}
}
}
私有列表InitTableCells()
{
_单元格=新列表();
foreach(组任务列表中的组任务列表)
{
foreach(组任务列表中的WeekItemTaskViewModel项)
{
var tableCell=new TableSectionItemViewModel(){TaskViewModel=item};
_单元格。添加(表格单元格);
}
}
返回单元;
}
私有void DoTableSelect(TableSectionItemViewModel tableItemViewModel)
{
/*
所选字符串=tableItemViewModel.Title;
((WelcomeScreenViewModel)ViewModel)。SortViews(选定);
_titleLabel.Text=选中;
*/
int段=0;
int行=0;
nsindepath索引=null;
foreach(组任务的_列表中的变量组)
{
if(group.Contains(tableItemViewModel.TaskViewModel)){
行=group.IndexOf(tableItemViewModel.TaskViewModel);
index=nsindepath.FromRowSection(行,节);
打破
}
第++;
}
如果(索引!=null){
_table.TableView.DeselectRow(index,false);}
}
私有类TabeViewSource:MVXBindableTableTableViewSource
{
私人名单(首长名单);;
私人名单(listofooter);;
私有列表_组任务列表;
公共选项卡ElViewSource(UITableView视图、标题列表、文件夹列表、组任务列表)
:底座(视图)
{
_listOfFooter=listOfFooter;
_listOfGroupedTasks=listOfGroupedTasks;
_领导名单=领导名单;
}
公共优先权
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"); }
}
Group
    Session
    Session
    Session
Group
    Session
    Session
    Session
    Session
Group
    Session
    Session
etc
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;
}