Enabling and Configuring Context Menus
Send Feedback

Glossary Item Box

Topic Overview

Purpose

This topic provides a conceptual overview of using context menus with cells in the IGGridView™ control.

In this topic


Introduction

Context menus configuration summary

The IGGridView control allows you to specify a custom context menu for a cell that appears after a long-press. This is a notable distinction from the UITableView control, which does not allow this option.

Here is how it works:
You first access and add your item to the UIMenuController. When the user long-presses a cell, the IGGridView calls the IGGridViewDelegate’s shouldShowMenuForCellAtPath protocol method to determine whether to show the context menu. If the shouldShowMenuForCellAtPath protocol method returns YES, then the IGGridView calls the IGGridViewDelegate’s canPerformAction protocol method to determine whether the context menu can be shown at this time.


Creating a Context Menu and Handling Item Selection – Code Example

Description

The following code uses the IGGridViewDelegate to demonstrate how to create a context menu and handle a context menu selection.

Code

In Objective-C:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Code...
   
    // Add your custom menu item to the Context Menu
    UIMenuController *menu = [UIMenuController sharedMenuController];
    UIMenuItem *testItem = [[UIMenuItem alloc] initWithTitle:@"Send Email" action:@selector(sendEmail:)]; 
    [menu setMenuItems:[NSArray arrayWithObjects:testItem, nil]];
    gridView.delegate = self;
    // Code...
}
-(BOOL)gridView:(IGGridView*)gridView shouldShowMenuForCellAtPath:(IGCellPath*)path
{
    // Return yes, if a cell supports the menu being displayed
    return YES;
}
-(BOOL)gridView:(IGGridView *)gridView canPerformAction:(SEL)action forCellAtPath:(IGCellPath *)path withSender:(id)sender
{
    // Return yes for every menu item that you want to be displayed, in this case, we just want our custom one.
    return (action == @selector(sendEmail:)); 
}
-(void)gridView:(IGGridView *)gridView performAction:(SEL)action forCellAtPath:(IGCellPath *)path withSender:(id)sender
{
    if(action == @selector(sendEmail:))
        [self sendEmail:path];
}

In C#:

 public class MenuDelegate : IGGridViewDelegate
 {
  public override bool ShouldShowMenuForCell (IGGridView gridView, IGCellPath path)
  {
   return true;
  }

  public override bool CanPerformActionForCell (IGGridView gridView, Selector action, IGCellPath path, NSObject sender)
  {
   return action == new Selector("sendEmail");
  }

  public override void PerformActionForCell (IGGridView gridView, Selector action, IGCellPath path, NSObject sender)
  {
   if(action == new Selector("sendEmail"))
   {
    // Send E-mail
   }
  }
 }

 public class ViewController : UIViewController
 {
  public override void ViewDidLoad ()
  {
   base.ViewDidLoad ();
   IGGridView gridView = new IGGridView(new RectangleF(0,0, this.View.Frame.Size.Width, this.View.Frame.Size.Height), IGGridViewStyle.IGGridViewStyleDefault);
   gridView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
  
   gridView.WeakDataSource = new DataProvider();
  
   this.View.AddSubview(gridView);


   UIMenuController menu = UIMenuController.SharedMenuController;
   UIMenuItem testItem = new UIMenuItem("Send E-mail", new Selector("sendEmail"));
   menu.MenuItems = new UIMenuItem[]{testItem};

   gridView.WeakDelegate = new MenuDelegate();
  }
 }


Related Content

Topics

The following topic provides additional information related to this topic.

Topic

Purpose

The topics in this group cover how to enable, configure, and use the features supported by the IGGridView™ control.