iOS5上的UITableView新API
作者:DEVDIV
今天看到UITableView關于iOS5新增的API,有三個關于將UIMenuViewController使用到UITableViewCell上,以前還為讓Cell實現這個功能糾結過,哈哈,一起看看·
- - (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];
- }
- }
如果希望只出現一個或兩個特定的菜單選項,可以在- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 中加入相應判斷,return YES則出現,return NO則不出現了。
第三個函數就是我們處理邏輯的地方,我這里只是測試簡單純文本Cell。
這樣長按就可以出現三個功能菜單選項,點擊進行相應操作。
【編輯推薦】
責任編輯:冰凝兒
來源:
DEVDIV博客