0
votes

I want to override the below method in the custom renderer. All i want to do is show or hide the checkmark on select and deselect.

public override void RowSelected (UITableView tableView, NSIndexPath   indexPath)
{
 // Do something
}

Custom Renderer code below

 public class StandardViewCellRenderer : ViewCellRenderer
 {
    public override UIKit.UITableViewCell GetCell(Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);
        switch (item.StyleId)
        {
            case "checkmark":
                cell.Accessory = UIKit.UITableViewCellAccessory.Checkmark;
                break;
            default:
                cell.Accessory = UIKit.UITableViewCellAccessory.DisclosureIndicator;
                break;
        }
        return cell;
    }

i want to do something like below. What method should i override to achieve this.

    public override UIKit.UITableViewCell RowSelected(Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);
        cell.Accessory=UIKit.UITableViewCellAccessory.Checkmark;
        return cell;
     }


}

How to achieve this?

1

1 Answers

0
votes

The easiest way is to use a UITableViewDelegate subclass in a custom ListViewRenderer:

public class CheckedListViewRenderer : ListViewRenderer
{
    class CheckedUITableViewDelegate : UITableViewDelegate
    {
        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.CellAt(indexPath);
            cell.Accessory = UITableViewCellAccessory.Checkmark;
        }

        public override void RowDeselected(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.CellAt(indexPath);
            if (cell != null) cell.Accessory = UITableViewCellAccessory.None;
        }
    }

    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
    {
        base.OnElementChanged(e);
        if (Control?.Delegate == null)
        {
            Control.Delegate = new CheckedUITableViewDelegate();
        }
    }
}

enter image description here

Note:

This selection method of the accessory view does not take into account selected cells scrolling out of the view, multi-selection, nor does it take cell recycling into consideration either. You would need to maintain a secondary data source upon selection and deselection to handle those three items...