UITableViewCell をサブクラス化する代わりに、セルがタップされたときにコピー メニューを実装する簡単な方法はありますか?
ありがとう、
RL
UITableViewCell をサブクラス化する代わりに、セルがタップされたときにコピー メニューを実装する簡単な方法はありますか?
ありがとう、
RL
iOS 5 では、UITableViewDelegate メソッドを実装するのが簡単な方法です。
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
3 つのデリゲートを実装することで、長押しジェスチャの後に UIMenuController を呼び出すことができます。次のような例:
/**
allow UIMenuController to display menu
*/
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
/**
allow only action copy
*/
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
return action == @selector(copy:);
}
/**
if copy action selected, set as cell detail text
*/
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == @selector(copy:))
{
UITableViewCell* cell = [tableView cellForIndexPath:indexPath];
[[UIPasteboard generalPasteboard] setString:cell.detailTextLabel.text];
}
}
はい!
内部から呼び出します[[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani]
(コントローラーをアニメーション化する必要があるかどうかani
を決定する場所) ( UITableView のデリゲート メソッド)BOOL
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
編集: の「コピー」コマンドはUIMenuController
、デフォルトではdetailTextLabel.text
テキストをコピーしません。ただし、回避策があります。次のコードをクラスに追加します。
-(void)copy:(id)sender {
[[UIPasteboard generalPasteboard] setString:detailTextLabel.text];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == @selector(copy:)) {
return YES;
}
else {
return [super canPerformAction:action withSender:sender];
}
}