【发布时间】:2014-05-10 22:36:19
【问题描述】:
您好,我在尝试将 MvxTableViewCell 附件复选标记绑定到本地属性时遇到问题。我已经尝试过在 Bind MvxBindableTableViewCell's Accessory to boolean
找到的示例我对 IOS 很陌生,甚至对 MvvmCross 也很陌生,所以如果我犯了任何愚蠢的错误,我深表歉意
public partial class TaxaListCellView : MvxTableViewCell
{
public static readonly UINib Nib = UINib.FromName ("TaxaListCellView", NSBundle.MainBundle);
public static readonly NSString Key = new NSString ("TaxaListCellView");
public TaxaListCellView (IntPtr handle) : base (handle)
{
Accessory = UITableViewCellAccessory.Checkmark;
SelectionStyle = UITableViewCellSelectionStyle.None;
this.DelayBind (() => {
var set = this.CreateBindingSet<TaxaListCellView, TaxonViewModel>();
set.Bind(lblSelectedTaxon).To(s => s.Name);
//I've been playing around with both ways below, and a few different
//variants without any success
//set.Bind("IsChecked").To(s => s.IsSelected).TwoWay();
//set.Bind(@"'IsChecked':{'Path':'IsSelected'");
set.Apply();
});
}
public bool IsChecked
{
get { return Accessory == UITableViewCellAccessory.Checkmark; }
set { Accessory = value ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None; }
}
public static TaxaListCellView Create ()
{
return (TaxaListCellView)Nib.Instantiate (null, null) [0];
}
public override void SetSelected (bool selected, bool animated)
{
Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None;
base.SetSelected (selected, animated);
}
}
我不确定我的 MvxTableViewController 是否有任何问题,但这是代码
public class TaxaListView : MvxTableViewController
{
public TaxaListView()
{
Title = "Taxon List";
}
private UISearchBar _searchBar;
public override void ViewDidLoad()
{
base.ViewDidLoad();
_searchBar = new UISearchBar(new RectangleF(0,0,320, 44))
{
AutocorrectionType = UITextAutocorrectionType.Yes,
};
_searchBar.SearchButtonClicked += SearchBar_SearchButtonClicked;
_searchBar.TextChanged += SearchBarOnTextChanged;
var source = new MvxSimpleTableViewSource(TableView, TaxaListCellView.Key, TaxaListCellView.Key);
var set = this.CreateBindingSet<TaxaListView, TaxaListViewModel> ();
set.Bind (source).To (vm => vm.Taxa);
set.Bind (source)
.For (s => s.SelectionChangedCommand)
.To (vm => vm.ItemSelectedCommand);
set.Apply ();
TableView.RowHeight = 50;
TableView.Source = source;
TableView.AllowsSelection = true;
TableView.AllowsSelectionDuringEditing = true;
TableView.TableHeaderView = _searchBar;
TableView.ReloadData();
}
private void SearchBarOnTextChanged(object sender, UISearchBarTextChangedEventArgs uiSearchBarTextChangedEventArgs)
{
if(string.IsNullOrWhiteSpace(_searchBar.Text))
{
((TaxaListViewModel) ViewModel).SearchTaxaByText(string.Empty);
}
}
void SearchBar_SearchButtonClicked(object sender, System.EventArgs e)
{
((TaxaListViewModel)ViewModel).SearchTaxaByText(_searchBar.Text);
}
}
当我首先从列表中选择一个项目时 当我开始搜索时,或者即使我回到项目列表也会发生
【问题讨论】:
-
你确定你的 UITableView 允许选择吗?当您选择其中一行时,SetSelected 是否会触发?
-
嗨@choper,是的,很抱歉,这行得通,如果我返回列表时我丢失了复选标记,或者如果我使用搜索栏我丢失了选定的复选标记。我将发布两个屏幕截图以更好地解释它。感谢回复
-
尝试告诉 ViewModel 属性已更改 - 添加
public event EventHandler IsChecked;然后在属性更改时从单元格中触发它 - 例如在SetSelected期间。这可能工作(没有它 - 没有任何东西告诉 Cell 的 ViewModel 该项目已更改) -
尝试告诉 ViewModel 属性已更改 - 添加
public event EventHandler IsChecked;然后在属性更改时从单元格中触发它 - 例如在SetSelected -
@Stuart 感谢您的帮助,您的回答引发了一些问题,本质上是根本问题。我将在下面发布我的答案。可能有更好的方法来做我正在做的事情,但它确实有效。再次感谢
标签: uitableview xamarin.ios mvvmcross