Okay. In the previous article, we talked about generating a clipboard on UILabel. In this article, we will write two ways to use the clipboard on UITableView;
I. Use in custom Cells
In fact, cell usage is basically the same as label usage.
1. Release Method:
-(BOOL) canBecomeFirstResponder {
Return YES;
}
-(BOOL) can1_maction :( SEL) action withSender :( id) sender {
If (action ==@ selector (cut :)){
Return NO;
}
Else if (action ==@ selector (copy :)){
Return YES;
}
Else if (action ==@ selector (paste :)){
Return NO;
}
Else if (action ==@ selector (select :)){
Return NO;
}
Else if (action ==@ selector (selectAll :)){
Return NO;
}
Else {
Return [super canPerformAction: action withSender: sender];
}
}
Then, of course, the copy method is implemented ..
-(Void) copy :( id) sender {
NSLog (@ "copy ");
UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
[Pasteboard setString: [[self textLabel] text];
}
However, the most important step is to use this method to stimulate the copy menu.
-(Void) setHighlighted :( BOOL) highlighted animated :( BOOL) animated {
[[Self delegate] performSelector: @ selector (showMenu :)
WithObject: self afterDelay: 0.9f];
[Super setHighlighted: highlighted animated: animated];
}
As for the Menu Generation, you should have read the previous article and are familiar with the code:
// Display menu
-(Void) showMenu :( id) cell {
If ([cell isHighlighted]) {
[Cell becomeFirstResponder];
UIMenuController * menu = [UIMenuController sharedMenuController];
[Menu setTargetRect: [cell frame] inView: [self view];
[Menu setMenuVisible: YES animated: YES];
}
}
Okay, the rest is about how to use a custom cell. You should be familiar with it. I will not introduce it one by one. Since then, the first method of using clipboard has been introduced.
II. The second method is simpler, because (_ MAC_NA ,__ IPHONE_5_0) three methods have been released, which is very easy to use. I will directly use Farah above.
-(BOOL) tableView :( UITableView *) tableView shouldShowMenuForRowAtIndexPath :( NSIndexPath *) indexPath {
Return YES;
}
-(BOOL) tableView :( UITableView *) tableView can1_maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {
If (action ==@ selector (copy :)){
Return YES;
}
Return NO;
}
-(Void) tableView :( UITableView *) tableView upload maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {
UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];
If (action ==@ selector (copy :)){
[UIPasteboard generalPasteboard]. string = cell. textLabel. text;
}
}
With the above three methods, I think you can easily use the clipboard in the cell ..
Well, the second article is complete. Continue to focus on customizing the clipboard ----- see the next version.