Ios development Implementation of UITableViewCell pop-up UIMenuController

Source: Internet
Author: User

This function is required in the project. I found some documents on the Internet, and some of them are not very clear. The implementation steps are as follows:

1. Add UILongPressGestureRecognizer to the cell and handle the event accordingly.

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath
*) IndexPath

{

..............

UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: selfaction: @ selector (cellLongPress :)];

[Cell addGestureRecognizer: longPressGesture];

Return cell;

}

2. Configure and display UIMenuController

-(Void) cellLongPress UIGestureRecognizer *) recognizer {

If (recognizer. state =
UIGestureRecognizerStateBegan ){


CGPoint location = [recognizer
LocationInView: self];


NSIndexPath * indexPath = [self
IndexPathForRowAtPoint: location];

UIMyTableViewCell * cell = (UIMyTableViewCell *) recognizer. view;

// Use cell as the first response here (cell cannot be a responder by default, and the canBecomeFirstResponder method needs to be rewritten)

[Cell becomeFirstResponder];


UIMenuItem * itCopy = [UIMenuItem
Alloc] initWithTitle: @ "copy"
Action: @ selector (handleCopyCell :)];


UIMenuItem * itDelete = [UIMenuItem
Alloc] initWithTitle: @ "delete"
Action: @ selector (handleDeleteCell :)];

UIMenuController * menu = [UIMenuController
SharedMenuController];

[Menu setMenuItems: [NSArray arrayWithObjects: itCopy,
ItDelete, nil];

[Menu
SetTargetRect: cell. frame inView: self];

[Menu setMenuVisible: YES
Animated: YES];

[ItCopy release];

[ItDelete release];

}

}

-(Void) handleCopyCell :( id) sender {// copy cell

NSLog (@ "handle copy cell ");

}

-(Void) handleDeleteCell :( id) sender {// Delete cell

NSLog (@ "handle delete cell ");

}

3. Rewrite the canBecomeFirstResponder method in the Custom cell and return yes.

// In order to display the menu, the target view must be in the responder chain. Many UIKit views cannot be a responder by default. Therefore, you need to make these views reload the canBecomeFirstResponder method and return YES

-(BOOL) canBecomeFirstResponder {

Return
YES;

}


After these steps, you will be able to see this foreign language on the Internet and share it with us:

Http://www.intridea.com/blog/2010/12/22/developers-notes-for-uimenucontroller


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.