Created
November 20, 2012 17:23
-
-
Save slodge/4119401 to your computer and use it in GitHub Desktop.
MvxActionBasedBindableTableViewSource fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Copyright | |
// <copyright file="MvxActionBasedBindableTableViewSource.cs" company="Cirrious"> | |
// (c) Copyright Cirrious. http://www.cirrious.com | |
// This source is subject to the Microsoft Public License (Ms-PL) | |
// Please see license.txt on http://opensource.org/licenses/ms-pl.html | |
// All other rights reserved. | |
// </copyright> | |
// | |
// Project Lead - Stuart Lodge, Cirrious. http://www.cirrious.com | |
#endregion | |
using System; | |
using System.Collections.Generic; | |
using Cirrious.MvvmCross.Binding.Interfaces; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
namespace Cirrious.MvvmCross.Binding.Touch.Views | |
{ | |
public class MvxActionBasedBindableTableViewSource : MvxBindableTableViewSource | |
{ | |
static readonly NSString DefaultCellIdentifier = new NSString("MvxDefaultBindableTableViewCell"); | |
protected MvxActionBasedBindableTableViewSource(UITableView tableView) | |
: base(tableView) | |
{ | |
Initialise(); | |
} | |
public MvxActionBasedBindableTableViewSource(UITableView tableView, UITableViewCellStyle style, NSString cellIdentifier, string bindingText, UITableViewCellAccessory tableViewCellAccessory) | |
: base(tableView, style, cellIdentifier, bindingText, tableViewCellAccessory) | |
{ | |
Initialise(); | |
} | |
public MvxActionBasedBindableTableViewSource(UITableView tableView, UITableViewCellStyle style, NSString cellIdentifier, IEnumerable<MvxBindingDescription> descriptions, UITableViewCellAccessory tableViewCellAccessory) | |
: base(tableView, style, cellIdentifier, descriptions, tableViewCellAccessory) | |
{ | |
Initialise(); | |
} | |
private void Initialise() | |
{ | |
CellCreator = CreateDefaultBindableCell; | |
CellModifier = (ignored) => { }; | |
} | |
public Func<UITableView, NSIndexPath, object, MvxBindableTableViewCell> CellCreator { get; set; } | |
public Action<MvxBindableTableViewCell> CellModifier { get; set; } | |
public Func<NSString> CellIdentifierOverride { get; set; } | |
protected override NSString CellIdentifier | |
{ | |
get | |
{ | |
if (CellIdentifierOverride != null) | |
return CellIdentifierOverride(); | |
return base.CellIdentifier; | |
} | |
} | |
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item) | |
{ | |
var reuse = tableView.DequeueReusableCell(CellIdentifier); | |
if (reuse != null) | |
return reuse; | |
var cell = CellCreator(tableView, indexPath, item); | |
if (CellModifier != null) | |
CellModifier(cell); | |
return cell; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Copyright | |
// <copyright file="MvxBaseBindableTableViewSource.cs" company="Cirrious"> | |
// (c) Copyright Cirrious. http://www.cirrious.com | |
// This source is subject to the Microsoft Public License (Ms-PL) | |
// Please see license.txt on http://opensource.org/licenses/ms-pl.html | |
// All other rights reserved. | |
// </copyright> | |
// | |
// Project Lead - Stuart Lodge, Cirrious. http://www.cirrious.com | |
#endregion | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Windows.Input; | |
using Cirrious.MvvmCross.Binding.Interfaces; | |
using Cirrious.MvvmCross.Binding.Interfaces.Binders; | |
using Cirrious.MvvmCross.Binding.Touch.Interfaces.Views; | |
using Cirrious.MvvmCross.Commands; | |
using Cirrious.MvvmCross.ExtensionMethods; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
namespace Cirrious.MvvmCross.Binding.Touch.Views | |
{ | |
public abstract class MvxBaseBindableTableViewSource : UITableViewSource | |
{ | |
private static readonly NSString DefaultCellIdentifier = new NSString("BindableTableViewCell"); | |
private static readonly MvxBindingDescription[] DefaultBindingDescription = new MvxBindingDescription[] | |
{ | |
new MvxBindingDescription() | |
{ | |
TargetName = "TitleText", | |
SourcePropertyPath = string.Empty | |
}, | |
}; | |
private readonly IEnumerable<MvxBindingDescription> _bindingDescriptions; | |
private readonly NSString _cellIdentifier; | |
private readonly UITableViewCellStyle _cellStyle; | |
private readonly UITableView _tableView; | |
private readonly UITableViewCellAccessory _tableViewCellAccessory = UITableViewCellAccessory.None; | |
protected virtual NSString CellIdentifier | |
{ | |
get { return _cellIdentifier; } | |
} | |
protected MvxBaseBindableTableViewSource(UITableView tableView) | |
: this(tableView, UITableViewCellStyle.Default, DefaultCellIdentifier, DefaultBindingDescription) | |
{ | |
} | |
protected MvxBaseBindableTableViewSource(UITableView tableView, UITableViewCellStyle style, NSString cellIdentifier, string bindingText, UITableViewCellAccessory tableViewCellAccessory = UITableViewCellAccessory.None) | |
: this(tableView, style, cellIdentifier, ParseBindingText(bindingText), tableViewCellAccessory) | |
{ | |
} | |
protected MvxBaseBindableTableViewSource(UITableView tableView, UITableViewCellStyle style, NSString cellIdentifier, IEnumerable<MvxBindingDescription> descriptions, UITableViewCellAccessory tableViewCellAccessory = UITableViewCellAccessory.None) | |
{ | |
_tableView = tableView; | |
_cellStyle = style; | |
_cellIdentifier = cellIdentifier; | |
_bindingDescriptions = descriptions; | |
_tableViewCellAccessory = tableViewCellAccessory; | |
} | |
protected IEnumerable<MvxBindingDescription> BindingDescriptions { get { return _bindingDescriptions; } } | |
protected UITableView TableView { get { return _tableView; } } | |
private static IEnumerable<MvxBindingDescription> ParseBindingText(string bindingText) | |
{ | |
if (string.IsNullOrEmpty(bindingText)) | |
return DefaultBindingDescription; | |
return MvxServiceProviderExtensions.GetService<IMvxBindingDescriptionParser>().Parse(bindingText); | |
} | |
public event EventHandler<MvxSimpleSelectionChangedEventArgs> SelectionChanged; | |
public ICommand SelectionChangedCommand { get; set; } | |
public virtual void ReloadTableData() | |
{ | |
_tableView.ReloadData(); | |
} | |
protected virtual UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item) | |
{ | |
var reuse = tableView.DequeueReusableCell(CellIdentifier); | |
if (reuse != null) | |
return reuse; | |
return CreateDefaultBindableCell(tableView, indexPath, item); | |
} | |
protected virtual MvxBindableTableViewCell CreateDefaultBindableCell(UITableView tableView, NSIndexPath indexPath, object item) | |
{ | |
return new MvxBindableTableViewCell(_bindingDescriptions, _cellStyle, CellIdentifier, | |
_tableViewCellAccessory); | |
} | |
protected abstract object GetItemAt(NSIndexPath indexPath); | |
public override void RowSelected(UITableView tableView, NSIndexPath indexPath) | |
{ | |
var item = GetItemAt(indexPath); | |
var selectionChangedArgs = MvxSimpleSelectionChangedEventArgs.JustAddOneItem(item); | |
var handler = SelectionChanged; | |
if (handler != null) | |
handler(this, selectionChangedArgs); | |
var command = SelectionChangedCommand; | |
if (command != null) | |
command.Execute(item); | |
} | |
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) | |
{ | |
var item = GetItemAt(indexPath); | |
var cell = GetOrCreateCellFor(tableView, indexPath, item); | |
var bindable = cell as IMvxBindableView; | |
if (bindable != null) | |
bindable.BindTo(item); | |
return cell; | |
} | |
public override int NumberOfSections(UITableView tableView) | |
{ | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment