【发布时间】:2016-12-19 20:00:43
【问题描述】:
我有 UITableView,每个单元格都有按钮和文本。实际的单元格不应该是可点击的,但按钮应该是可点击的。当我在模拟器中运行时,我可以点击按钮,但是当部署到设备时它被禁用。 (已编辑)
尝试按照这篇帖子Button in UITableViewCell not responding under ios 7 中的许多不同步骤来修复它,例如 ContentView.UserInteractionEnabled = False;用于单元格。
所有这些都适用于原生 iOS,也许对于 Xamarin 还有其他东西?
任何想法我错过了什么?
UITableViewCell 代码
this.DelayBind(() =>
{
var set = this.CreateBindingSet<CalendarCell, ActivityModel>();
set.Bind(CustomerNameLabel).To(a => a.Subject);
set.Bind(MarkAsMeetingButton).For(a => a.Hidden).WithConversion("ActivityTypeToVisibility");
set.Bind(MarkAsMeetingButton).To(a => a.SendMessageToViewModelCommand).CommandParameter(BindingContext);
set.Apply();
});
ContentView.UserInteractionEnabled = false;
UItableView 代码
public override void ViewDidLoad()
{
base.ViewDidLoad();
var source = new TableSource(CalendarList);
var set = this.CreateBindingSet<CalendarView, CalendarViewModel>();
set.Bind(source).To(vm => vm.CalendarList);
set.Apply();
CalendarList.Source = source;
CalendarList.ReloadData();
}
public class TableSource : MvxSimpleTableViewSource
{
private List<IGrouping<DateTime, ActivityModel>> GroupedCalendarList = new List<IGrouping<DateTime, ActivityModel>>();
public TableSource(UITableView calendarView) : base(calendarView, "CalendarCell", "CalendarCell")
{}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var currentSection = GroupedCalendarList[indexPath.Section].ToList();
var item = currentSection[indexPath.Row];
var cell = GetOrCreateCellFor(tableView, indexPath, item);
var bindable = cell as IMvxDataConsumer;
if (bindable != null)
bindable.DataContext = item;
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return (nint)GroupedCalendarList[(int)section].Count();
}
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
var label = new UILabel();
var currentDate = DateTime.Now;
var titleText = GroupedCalendarList[(int)section].FirstOrDefault().ScheduledStart.Value.Date;
return label;
}
public override nint NumberOfSections(UITableView tableView)
{
return (nint)GroupedCalendarList.Count;
}
public override void ReloadTableData()
{
if (ItemsSource == null) return;
var groupedCalendarList = (ItemsSource as List<ActivityModel>).GroupBy(cl => cl.ScheduledStart.Value.Date).ToList();
GroupedCalendarList = new List<IGrouping<DateTime, ActivityModel>>(groupedCalendarList);
base.ReloadTableData();
}
}
此代码在模拟器上工作得非常好,但在设备上不起作用,每个单元格中的 UIButton 都被禁用。
【问题讨论】:
-
你能发布你正在使用的代码吗? UITableView、UITableViewCell 以及可能绑定到的 ViewModel 成员都会很有用。
-
添加了一些代码,VM有点复杂,但应该没有问题
-
您的模拟器和设备运行的是什么版本的 iOS?
ContentView.UserInteractionEnabled = false;是单元格中的按钮不可点击的原因;对于ContentView的所有子视图,UserInteractionEnabled为 false。您需要一种替代解决方案来禁用对单元格的单击,同时允许单击按钮 - 请参阅 Xamarin 论坛上的建议 here
标签: xamarin xamarin.ios mvvmcross