iOS5上的UITableView新API
作者:DEVDIV 
  今天看到UITableView關(guān)于iOS5新增的API,有三個關(guān)于將UIMenuViewController使用到UITableViewCell上,以前還為讓Cell實現(xiàn)這個功能糾結(jié)過,哈哈,一起看看·
 
- - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
 - {
 - return YES;
 - }
 - - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
 - {
 - return YES;
 - }
 - - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
 - {
 - if (action == @selector(copy:)) {
 - [UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];
 - }
 - if (action == @selector(cut:)) {
 - [UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];
 - [dataArray replaceObjectAtIndex:indexPath.row withObject:@""];
 - [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
 - }
 - if (action == @selector(paste:)) {
 - NSString *pasteString = [UIPasteboard generalPasteboard].string;
 - NSString *tmpString = [NSString stringWithFormat:@"%@%@",[dataArray objectAtIndex:indexPath.row],pasteString];
 - [dataArray replaceObjectAtIndex:indexPath.row withObject:tmpString];
 - [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
 - }
 - }
 
如果希望只出現(xiàn)一個或兩個特定的菜單選項,可以在- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 中加入相應(yīng)判斷,return YES則出現(xiàn),return NO則不出現(xiàn)了。
第三個函數(shù)就是我們處理邏輯的地方,我這里只是測試簡單純文本Cell。
這樣長按就可以出現(xiàn)三個功能菜單選項,點擊進行相應(yīng)操作。
【編輯推薦】
責(zé)任編輯:冰凝兒 
                    來源:
                    DEVDIV博客
 














 
 
 
 
 
 
 