2

I have UITableView and each cell has button and text. Actual cell shouldn't be clickable, but button should. When I run in simulator I can hit button, but when deploy to device it is disabled. (edited)

Tried to follow many different steps to fix it from this post Button in UITableViewCell not responding under ios 7, such as ContentView.UserInteractionEnabled = False; for cell.

All of those for native iOS, maybe for Xamarin there are something else?

Any ideas what am I missing?

UITableViewCell code

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 code

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();
        }
    }

This code works absolutely fine for simulator, but doesn't work on device, UIButton in each cell is disabled.

Community
  • 1
  • 1
  • Can you post the code you're using? The UITableView, UITableViewCell and potentially the ViewModel members you're binding to would be useful. – Luke Pothier Dec 19 '16 at 21:14
  • Added some code, VM is a bit complicated, but there shouldn't be any problems – Oleksandr Gridin Dec 19 '16 at 22:39
  • 1
    What versions of iOS are your simulator and device running? `ContentView.UserInteractionEnabled = false;` is the reason the button in your cell is not clickable; `UserInteractionEnabled` is false for all subviews of `ContentView`. You'll need an alternative solution to disabling the click on the cells while allowing the button to be clicked -- see the suggestions on the Xamarin forums [here](https://forums.xamarin.com/discussion/15560/how-to-add-a-custom-button-in-a-table-cell) – Luke Pothier Dec 20 '16 at 18:06

1 Answers1

2

As @Luke said in the comments of your question said, do not use ContentView.UserInteractionEnabled = false;, since this disables any touch events on the whole view of your cell.

To achieve what you need, implement the UITableViewDelegate method ShouldHighlightRow and return false:

public override bool ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath) { return false; }

Then, the cells will not get highlighted on tap, and the RowSelected method will not get called, but your button will be clickable!

Lucas P.
  • 4,282
  • 4
  • 29
  • 50