xamarin - UITableViewCell Xamarin.iOs 中的禁用按钮

标签 xamarin xamarin.ios mvvmcross

我有 UITableView,每个单元格都有按钮和文本。实际的单元格不应该是可点击的,但按钮应该是可点击的。当我在模拟器中运行时,我可以点击按钮,但是当部署到设备时,它被禁用。 (已编辑)

尝试按照这篇文章中的许多不同步骤来修复它 Button in UITableViewCell not responding under ios 7 ,如ContentView.UserInteractionEnabled = False;对于细胞。

所有这些都适用于 native 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 都被禁用。

最佳答案

正如 @Luke 在您问题的评论中所说,不要使用 ContentView.UserInteractionEnabled = false;,因为这会禁用单元格整个 View 上的任何触摸事件。

要实现您的需要,请实现 UITableViewDelegate 方法 ShouldHighlightRow 并返回 false:

公共(public)覆盖 bool ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath) { 返回假; }

然后,点击时单元格将不会突出显示,并且 RowSelected 方法将不会被调用,但您的按钮将可单击!

关于xamarin - UITableViewCell Xamarin.iOs 中的禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41230114/

相关文章:

c# - 如何在自定义选择器中设置和获取选定值?该值不可更改

android - VS2017/Xamarin.Android : VS thinks it's still debugging after the emulator is closed

ios - 将来自麦克风的音频录制为 .wav 格式并使用 Xamarin.ios 中的 AudioUnit 进行播放

ios - CollapseLayers = true 时 iOS 中的核心绘图性能

android - 我可以在Xamarin之外使用MVVMCross吗?

ios - Visual Studio 2015 Xamarin iOS 菜单已禁用

c# - 找不到类型或命名空间名称 'EventHandler'

xcode - MonoTouch 应用程序构建

ios - 如何在 UiTableViewCell 中动态更改 ImageView?

android - 如何使用 MvxImageView.DefaultImagePath?