AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = naVC;
[rootVC release];
[naVC release];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
RootViewController.m
#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
[self.tableView release];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
// 添加一个长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[cell addGestureRecognizer:longPress];
[longPress release];
}
cell.textLabel.text = @"猞猁";
return cell;
}
// 长按的触发方法
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress {
// 根据手势对象找视图
UITableViewCell *cell = (UITableViewCell *)longPress.view;
// 创建快捷菜单
UIMenuController *menu = [UIMenuController sharedMenuController];
// 给快捷菜单进行定位显示
[menu setTargetRect:cell.frame inView:cell.superview];
// 让快捷菜单显示出来
[menu setMenuVisible:YES animated:YES];
// 自定义的功能
UIMenuItem *addItem = [[UIMenuItem alloc] initWithTitle:@"添加" action:@selector(addAction:)];
UIMenuItem *flagItem = [[UIMenuItem alloc] initWithTitle:@"flag" action:@selector(flagAction:)];
// 把功能添加到快捷菜单上
[menu setMenuItems:@[addItem, flagItem]];
[addItem release];
[flagItem release];
}
// 自定义的item必须实现方法, 才能在菜单上显示
- (void)addAction:(id)sender {
}
- (void)flagAction:(id)sender {
}
// 这个方法必须实现, 如果不实现, 快捷菜单无法显示
- (BOOL)canBecomeFirstResponder {
return YES;
}
// 只要重写系统的这些方法, 我们就可以在快捷菜单上显示
- (void)delete:(id)sender {
NSLog(@"delete");
}
- (void)copy:(id)sender {
NSLog(@"copy");
}
- (void)select:(id)sender {
NSLog(@"select");
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}