iOS5上的UITableView新API

移动开发 iOS
今天看到UITableView关于iOS5新增的API,有三个关于将UIMenuViewController使用到UITableViewCell上,以前还为让Cell实现这个功能纠结过,哈哈,一起看看·
  1. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return YES;  
  4. }  
  5. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender   
  6. {  
  7.     return  YES;  
  8. }  
  9. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender  
  10. {  
  11.     if (action == @selector(copy:)) {  
  12.         [UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];  
  13.     }  
  14.     if (action == @selector(cut:)) {  
  15.          [UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];  
  16.         [dataArray replaceObjectAtIndex:indexPath.row withObject:@""];  
  17.         [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];  
  18.     }  
  19.     if (action == @selector(paste:)) {  
  20.         NSString *pasteString = [UIPasteboard generalPasteboard].string;  
  21.         NSString *tmpString = [NSString stringWithFormat:@"%@%@",[dataArray objectAtIndex:indexPath.row],pasteString];  
  22.         [dataArray replaceObjectAtIndex:indexPath.row withObject:tmpString];  
  23.         [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];  
  24.     }  

如果希望只出现一个或两个特定的菜单选项,可以在- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 中加入相应判断,return YES则出现,return NO则不出现了。

第三个函数就是我们处理逻辑的地方,我这里只是测试简单纯文本Cell。

这样长按就可以出现三个功能菜单选项,点击进行相应操作。

【编辑推荐】

  1. ios5中UIViewController新使用方法
  2. iOS应用开发新手教程:iOS5 UIKit新特性
  3. 苹果iOS5.1安全漏洞,设备可由5.1降级5.0.1?
责任编辑:冰凝儿 来源: DEVDIV博客
相关推荐

2012-01-18 14:14:29

iOS教程iOS5

2012-01-18 13:51:39

2012-12-24 14:53:44

ios

2011-09-19 15:42:33

TwitteriOS5

2011-08-09 14:25:43

苹果iCloudiOS5

2013-03-25 13:41:10

iOS5ARC内存管理

2012-05-27 20:21:40

2011-10-05 01:51:20

iOS5苹果

2011-07-07 16:38:21

iOS UITableVie

2013-06-20 11:21:58

iOS开发UITableView

2011-06-08 10:29:37

开源闭源苹果

2011-06-07 06:59:51

iOS 5iOS苹果

2013-12-12 10:46:22

2011-05-31 22:53:14

乔布斯WWDCiCloud

2013-07-22 14:47:56

iOS开发iOS5中ASIHtt

2011-06-09 10:51:53

iPhone 3GSiOS5苹果

2011-06-07 07:06:33

iOS 5iOSWWDC

2011-10-19 16:19:27

iOS 5苹果

2011-11-01 16:50:25

iOS5苹果中国用户

2011-06-15 09:53:42

点赞
收藏

51CTO技术栈公众号