RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@end
@implementation RootViewController
- (void)dealloc
{
[_tableView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
1.创建列表视图
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
// self.tableView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.tableView];
[_tableView release];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
2.在cell种添加一个长按的手势.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[cell addGestureRecognizer:longPress];
[longPress release];
}
cell.textLabel.text = @"1";
return cell;
}
3.长按的触发方法
- (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:@"add" action:@selector(addAction:)];
UIMenuItem *flagItem = [[UIMenuItem alloc] initWithTitle:@"flag" action:@selector(flagAction:)];
// 把功能添加到快捷菜单上.
[menu setMenuItems:@[addItem, flagItem]];
[addItem release];
[flagItem release];
}
4.自定义的item必须实现方法,才能在菜单上显示.
- (void)addAction:(id)sander {
}
- (void)flagAction:(id)sander {
}
5.这个方法必须实现,如果不实现,快捷菜单无法显示
- (BOOL)canBecomeFirstResponder {
return YES;
}
6.只要重写系统的这些方法,我们就可以在快捷菜单上显示.
- (void)delete:(id)sender {
NSLog(@"delete");
}
- (void)copy:(id)sender {
NSLog(@"copy");
}
- (void)select:(id)sender {
NSLog(@"select");
}
UI_快捷菜单
最新推荐文章于 2022-09-25 10:13:34 发布