Cell display of UITableView in IOS development (27)

Source: Internet
Author: User

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:

 

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.