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