1 Preface
You can press the Cell length of UITableView to trigger shortcut menus, including copying, pasting, and other operations.
2. code example
ZYViewController. h
[Plain] view plaincopyprint? # Import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy
@ Property (nonatomic, strong) UITableView * myTableView;
@ End
# Import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy
@ Property (nonatomic, strong) UITableView * myTableView;
@ End
ZYViewController. m
[Plain] view plaincopyprint? @ Synthesize myTableView;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStylePlain]; // you can specify UITableViewStyleGrouped as the group mode.
Self. myTableView. delegate = self; // set the proxy to itself
MyTableView. dataSource = self; // set the data source to itself
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // ensure that the TablView can adjust the size correctly.
[Self. view addSubview: myTableView];
}
// Set the height of each row
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
CGFloat result = 20366f;
If ([tableView isEqual: self. myTableView]) {
// Result = 40366f;
Result = 80366f;
}
Return result;
}
// Set the number of rows displayed for each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return 3;
}
// Data in each row
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * tableViewCellIdentifier = @ "MyCells"; // you can specify the Cell id.
Result = [tableView dequeueReusableCellWithIdentifier: tableViewCellIdentifier]; // return a reusable table view cell object through the identifier
If (result = nil ){
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tableViewCellIdentifier]; // initialize a table cell style and reused identifier, and return it to the caller.
}
// IndexPath. section indicates the index indexPath. row of the section indicates the index of the number of rows.
Result. textLabel. text = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row];
}
Return result;
}
// Events triggered when a row is clicked
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
If ([tableView isEqual: myTableView]) {
NSLog (@ "% @", [NSString stringWithFormat: @ "Cell % ld in Section % ld is selected", (long) indexPath. row, (long) indexPath. section]);
}
}
// Enable long-pressed menu
-(BOOL) tableView :( UITableView *) tableView shouldShowMenuForRowAtIndexPath :( NSIndexPath *) indexPath {
Return YES;
}
// Each Action is allowed
-(BOOL) tableView :( UITableView *) tableView can1_maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {
NSLog (@ "% @", NSStringFromSelector (action ));
Return YES;
}
// Telling a given row indicates that the operation is copied or pasted,
-(BOOL) tableView :( UITableView *) tableView into maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {
If (action ==@ selector (copy :)) {// if the operation is a copy
UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];
UIPasteboard * pasteBoard = [UIPasteboard generalPasteboard]; // clipboard
[PasteBoard setString: cell. textLabel. text];
NSLog (@ "% @", pasteBoard. string); // obtain the clipboard content
Return YES;
}
Return NO;
}
@ Synthesize myTableView;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStylePlain]; // you can specify UITableViewStyleGrouped as the group mode.
Self. myTableView. delegate = self; // set the proxy to itself
MyTableView. dataSource = self; // set the data source to itself
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // ensure that the TablView can adjust the size correctly.
[Self. view addSubview: myTableView];
}
// Set the height of each row
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
CGFloat result = 20366f;
If ([tableView isEqual: self. myTableView]) {
// Result = 40366f;
Result = 80366f;
}
Return result;
}
// Set the number of rows displayed for each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return 3;
}
// Data in each row
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * tableViewCellIdentifier = @ "MyCells"; // you can specify the Cell id.
Result = [tableView dequeueReusableCellWithIdentifier: tableViewCellIdentifier]; // return a reusable table view cell object through the identifier
If (result = nil ){
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tableViewCellIdentifier]; // initialize a table cell style and reused identifier, and return it to the caller.
}
// IndexPath. section indicates the index indexPath. row of the section indicates the index of the number of rows.
Result. textLabel. text = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row];
}
Return result;
}
// Events triggered when a row is clicked
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
If ([tableView isEqual: myTableView]) {
NSLog (@ "% @", [NSString stringWithFormat: @ "Cell % ld in Section % ld is selected", (long) indexPath. row, (long) indexPath. section]);
}
}
// Enable long-pressed menu
-(BOOL) tableView :( UITableView *) tableView shouldShowMenuForRowAtIndexPath :( NSIndexPath *) indexPath {
Return YES;
}
// Each Action is allowed
-(BOOL) tableView :( UITableView *) tableView can1_maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {
NSLog (@ "% @", NSStringFromSelector (action ));
Return YES;
}
// Telling a given row indicates that the operation is copied or pasted,
-(BOOL) tableView :( UITableView *) tableView into maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {
If (action ==@ selector (copy :)) {// if the operation is a copy
UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];
UIPasteboard * pasteBoard = [UIPasteboard generalPasteboard]; // clipboard
[PasteBoard setString: cell. textLabel. text];
NSLog (@ "% @", pasteBoard. string); // obtain the clipboard content
Return YES;
}
Return NO;
}
Running result:
Console display (including the operation by pressing the Copy button ):
16:58:14. 609 UITableViewTest1 [1536: c07] _ insertImage:
16:58:14. 613 UITableViewTest1 [1536: c07] cut:
16:58:14. 615 UITableViewTest1 [1536: c07] copy:
16:58:14. 616 UITableViewTest1 [1536: c07] select:
16:58:14. 618 UITableViewTest1 [1536: c07] selectAll:
16:58:14. 620 UITableViewTest1 [1536: c07] paste:
16:58:14. 621 UITableViewTest1 [1536: c07] delete:
16:58:14. 624 UITableViewTest1 [1536: c07] _ promptForReplace:
16:58:14. 626 UITableViewTest1 [1536: c07] _ showTextStyleOptions:
16:58:14. 628 UITableViewTest1 [1536: c07] _ define:
16:58:14. 629 UITableViewTest1 [1536: c07] _ addShortcut:
16:58:14. 631 UITableViewTest1 [1536: c07] _ accessibilitySpeak:
16:58:14. 633 UITableViewTest1 [1536: c07] _ accessibilityspeak?ageselection:
16:58:14. 635 UITableViewTest1 [1536: c07] _ accessibilityPauseSpeaking:
16:58:14. 636 UITableViewTest1 [1536: c07] makeTextWritingDirectionRightToLeft:
16:58:14. 638 UITableViewTest1 [1536: c07] makeTextWritingDirectionLeftToRight:
16:58:42. 048 UITableViewTest1 [1536: c07] copy: