您的位置:首页 > 移动开发 > IOS开发

ios tableView那些事 (十二) 给 tableview 加个长按快捷菜单

2013-09-03 12:53 369 查看
//允许Menu菜单
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   YES;
}

//每个cell都可以点击出现Menu菜单
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{   
  //   return YES;  // 全部显示
   //显示 copy

 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];    

}  

}

-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    
    if (action ==@selector(copy:)) {
        
        [UIPasteboard  generalPasteboard].string = [arrayValueobjectAtIndex:indexPath.row];
        
    }
    
    if (action ==@selector(cut:)) {
        
        [UIPasteboard  generalPasteboard].string = [arrayValueobjectAtIndex:indexPath.row];
        
        [arrayValue  replaceObjectAtIndex:indexPath.rowwithObject:@""];
        
        [tableView
reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationNone];
        
    }
    
    if (action ==@selector(paste:)) {
        
        NSString *pasteString = [UIPasteboard  generalPasteboard].string;
        
        NSString *tmpString = [NSString  stringWithFormat:@"%@%@",[arrayValue  objectAtIndex:indexPath.row],pasteString];
        
        [arrayValue   replaceObjectAtIndex:indexPath.rowwithObject:tmpString];
        
        [tableView
reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationNone];
        
    }
    
    
}

效果如下图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitableview