2012-11-23 21 views
4

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

在我的数据中,我有1-2个元素的区段范围,我的tablesource有一个包含所有元素的列表(ItemsSource)。

它显示所有部分都正确,但是看起来对于每个部分来说,它从元素的总列表中取出元素0和/或1,并且不认为它是新的部分,如果它是新的部分索引应该有一个偏移量。但我可能是错的。

我应该根据部分是具有与元件的多个列表和SWITH的tablesource.Itemsource - 我试过这种方法但它wwnt的厄运螺旋:P

任何帮助apreciated :)

以下是我用于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; 
    } 
    } 
} 
+0

感谢Bjarke这个问题。你能扩展更多关于你的意思吗?“如果它是一个新的部分索引应该有一个偏移量” - 例如某种解释或图表,显示您所看到的以及您期望看到的内容。我目前使用的唯一部分是conf样本......也许我在那里做了错误的事情! – Stuart

+0

我斯图尔特我正在看你的例子。我相信我的问题可能是我有一个列表中的所有元素,而且他们没有分组。我应该有一个listource列表的列表。的ItemsSource? – Bjarke

+0

嗨斯图尔特。我忘了你的例子中的这一部分,因此它绑定到了错误的元素: 保护覆盖对象GetItemAt(NSIndexPath indexPath) if(_sessionGroups == null) return null; return _sessionGroups [indexPath.Section] [indexPath.Row]; } – Bjarke

回答

8

我不太了解您目前看到的问题。

但是,我可以解释如何在会议示例中使用BaseSessionListView.cs中的部分。

基本上,样品中的的ItemsSource是一个分组源 - 这样的视图模型代码为:

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

这意味着,我的暴露的ItemsSource有结构有点像:

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

的基于Section的表格源中的方法是:

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

这似乎工作....但我不知道它是否有助于您的问题?


如果我想添加一个头元素,那么我想我会只是改变了RowsInSectionGetItemAt方法,以适应这一点 - 加GetOrCreateCellFor返回头元素细胞太...但我猜还有其他的方法可以做到这一点吗?

+0

为什么不使用IGrouping呢? – Softlion

+0

也许你可以稍微扩展一下这个问题?请考虑在接近2年后,这个问题和答案可能不会是我记忆中的新鲜事:) – Stuart