首页 » iOS 开发 » cell长按 显示拷贝 删除 复制 menuItem

cell长按 显示拷贝 删除 复制 menuItem

作者: 低调的术木 分类: iOS 开发 发布时间: 2015-04-08 21:29 ė浏览 1,791 次 6没有评论

The UIResponderStandardEditActions informal protocol declares methods that responder classes should override to handle common editing commands invoked in the user interface, such as Copy, Paste, and Select.

Although this is an informal protocol—that is, a category declared on NSObject—it is recommended that responder classes (that is, immediate or distant ancestors of UIResponder) override its methods. Starting with the first responder, UIResponder looks for a responder object that can handle the method, and works up the responder chain from there. Responder classes may also implement the canPerformAction:withSender: method of UIResponder to disable or enable user-interface commands based on the context. The copy:, cut:, delete:, paste:, select:, and selectAll: methods are invoked when users tap the corresponding command in the menu managed by the UIMenuController shared instance.

主要由cupy:,cut:,paste:,select:,selectAll:几种方法。

必须实现的方法有如下3个:

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return YES;
    }
    else
    {
        return indexPath.row < [self.person.contact count]? YES : NO;
    }
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if (action == @selector(copy:)){
        return YES;
    }
    else if (action == @selector(delete:)) {
        return NO;
    }
    else if(action == @selector(cut:)){
        return NO;
    }
    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];
    }
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if ((indexPath.section == 1) && indexPath.row < [self.person.contact count] + 2) {
        NSString *stringToCopy = nil;
        if(indexPath.row == 0) {
            stringToCopy = self.person.department;
        }else if(indexPath.row == 1) {
            stringToCopy = self.person.jobTitle;
        }else {
            ContactDataModel *contact = [self.person.contact objectAtIndex:indexPath.row];
            stringToCopy = contact.cvalue;
        }
        
        if (stringToCopy && action == @selector(copy:)) {
            [UIPasteboard generalPasteboard].string = stringToCopy;
        }
    }
}

本文出自 蓝天笔记,转载时请注明出处及相应链接。

本文永久链接: http://ubluesky.com/archives/725

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Ɣ回顶部