0

I need the UIMenuItem to call a method inside the UITableViewCell. Something similar is possible in swift as explained here: https://stackoverflow.com/a/31358474/202179 .

At the moment I am only able to call the method inside the UIViewController from the UIMenuItem if I add an Export attribute to the method.

There is some trick in the export of this method in the cell, but I have failed to find it till now. I would expect this code to work, but it doesn't:

[Register("CustomCell")]
public class CustomCell : UITableViewCell
{
    public CustomCell(IntPtr intPtr) : base(intPtr)
    {
        var menuItem = new UIMenuItem("Menu Item", new ObjCRuntime.Selector("CustomCell.SeekSelectedText"));
        UIMenuController.SharedMenuController.MenuItems = new UIMenuItem[] { menuItem };
        UIMenuController.SharedMenuController.Update();
    }

    [Export("SeekSelectedText")]
    void SeekSelectedText()
    {
        //do something
    }
}

As said just quoting a different selector inside the UIViewController works, so the problem is that this selector doesn't appear to be recognized whatever I try.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57

1 Answers1

0

Here is my test and the custom UIMenuItem works fine.

First, define a custom cell class which inherits UITableViewCell

class MyCell: UITableViewCell
{
    public MyCell(UITableViewCellStyle style, string reuseIdentifier):base(style, reuseIdentifier)
    {

    }

    [Export("custom")]
    void Custom()
    {
        //do something
        Console.WriteLine("custom pop up");
    }

    public override bool CanPerform(Selector action, NSObject withSender)
    {
        if (action == new Selector("custom"))
            return true;
        else
            return false;
    }
}

Second, set the source of your UITableView

class MyTableViewSource : UITableViewSource
{
    int i = 0;
    // create custom cell
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        string cellStr = "cell";
        MyCell cell = (MyCell)tableView.DequeueReusableCell(cellStr);
        if (cell == null)
        {
            cell = new MyCell(UITableViewCellStyle.Subtitle, cellStr);
            cell.TextLabel.Text = $"User Name {i}";
            cell.DetailTextLabel.Text = $"details here... {i++}";
        }
        return cell;
    }
    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return 10;
    }
}

Then override the realted methods(e.g., ShouldShowMenu, CanPerformAction).

class MyTableViewDelegate : UITableViewDelegate
{
    public override bool ShouldShowMenu(UITableView tableView, NSIndexPath rowAtindexPath)
    {
        return true;
    }

    public override bool CanPerformAction(UITableView tableView, Selector action, NSIndexPath indexPath, NSObject sender)
    {
        return true;
    }

    public override void PerformAction(UITableView tableView, Selector action, NSIndexPath indexPath, NSObject sender)
    {

    }

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

Last, don't foget to add custom MenuItem to your UITableViewCell.

UIMenuController.SharedMenuController.MenuItems = new UIMenuItem[] {
    new UIMenuItem ("Custom", new Selector ("custom"))
};

Since UICollectionView has similar functions to UITableVIew, you can directly refer to the sample SimpleCollectionView.

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • No, there is the selectable text in each row so the many appears without any code when the text is selected. Thanks for trying to provide the alternative. I've clearly said that the menu appears so that it is not a problem. – Ivan Ičin May 07 '21 at 09:03
  • @IvanIčin What is "selectable text"? It has not be mentioned in you description or the link you provided. – 大陸北方網友 May 10 '21 at 02:10
  • If you long tap the selectable text control it selects the text and shows the menu. It was not mentioned but I said that there is the menu and that is the only way to show the menu without the code that you have provided. Possibly the question could be more elaborate, but you have ignored some facts quoted there, right? – Ivan Ičin May 11 '21 at 11:46
  • the selectable text control? Something like UITextField? Yep, it can show the copy menu without using any code. Do you want to do the same operation with the text in the Cell? Text partially selection, text copying? So how does this relate to the UIMenuItem and Selector you mentioned earlier? This is very confusing. – 大陸北方網友 May 12 '21 at 03:04
  • You understood it well. For your question I guess you need to look at the documentation, UIMenuItem would be a good start: https://developer.apple.com/documentation/uikit/uimenuitem . – Ivan Ičin May 12 '21 at 08:04