我有一个UITableView,它的每个单元都有一个CustomCheckBox(它是一个UIButton)和一个标签。我想要的是,当我单击Row(RowSelected方法)时,所述UIButton的Selected属性会发生变化,因此会更改所单击的UIButton的图像。
我有一个CustomCheckBox.cs
public partial class CustomCheckBox : UIButton
{
public CustomCheckBox(IntPtr handle) : base(handle)
{
this.SetImage(UIImage.FromBundle("ic_check_on"), UIControlState.Selected);
this.SetImage(UIImage.FromBundle("ic_check_off"), UIControlState.Normal);
this.TouchUpInside += (s, args) =>
{
this.Selected = !this.Selected;
};
}
}
ItemCellView.cs
public partial class ItemCellView : MvxTableViewCell
{
public static readonly NSString Key = new NSString("ItemCellView");
public static readonly UINib Nib;
static ItemCCellView()
{
Nib = UINib.FromName("ItemCellView", NSBundle.MainBundle);
}
public ItemCCellView(IntPtr handle) : base(handle)
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<ItemCellView, ItemViewModel>();
set.Bind(this.lblDescription).To(p => p.Description);
set.Apply();
});
}
}
和TableSource.cs
public class CoberturasOpcionalesSource : MvxTableViewSource
{
private PlanMulticoberturaOpcional SelectedItem;
readonly CotizarMulticoberturaViewModel ViewModel;
private UIViewController parentView;
public CoberturasOpcionalesSource(UITableView tableView, CotizarMulticoberturaViewModel viewModel) : base(tableView)
{
this.ViewModel = viewModel;
this.UseAnimations = true;
this.AddAnimation = UITableViewRowAnimation.Top;
this.RemoveAnimation = UITableViewRowAnimation.Middle;
tableView.EstimatedRowHeight = 30.0f;
tableView.RegisterNibForCellReuse(UINib.FromName(ItemCellView.Key, NSBundle.MainBundle), ItemCellView.Key);
}
public override nint NumberOfSections(UITableView tableView)
{
return this.ViewModel.ObsCollection.Count;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return 1;
}
protected override object GetItemAt(NSIndexPath indexPath)
{
return this.ViewModel.ObsCollection[indexPath.Section];
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow(indexPath, true);
var cell = (ItemCellView)tableView.DequeueReusableCell(ItemCellView.Key, indexPath);
cell.btnCheckBox.SendActionForControlEvents(UIControlEvent.TouchUpInside);
var item = this.ViewModel.ObsCollection[indexPath.Section];
if (item != null)
{
//
}
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
var cell = (ItemCellView)tableView.DequeueReusableCell(ItemCellView.Key, indexPath);
return cell;
}
}
我在CustomCheckbox.cs中的TouchUpInsde上有一个断点。当我触摸该行时,它会停在TouchUpInside上,但它没有做任何事情
感谢@ Garfield81。我在RowSelected方法上创建了一个新单元格。我改变了这个:
var cell = (ItemCellView)tableView.CellAt(indexPath);
答案 0 :(得分:2)
我认为问题在于您使用RowSelected
方法DequeResuableCell
来获取新单元格,而不是尝试访问已经显示的单元格。
相反,您需要使用cellForRow
方法(或Xamarin等效方法)来获取所选的单元格。