UIMenuController方法setTargetRect:inView:在UITableView中不起作用 [英] UIMenuController method setTargetRect:inView: not working in UITableView

查看:228
本文介绍了UIMenuController方法setTargetRect:inView:在UITableView中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 tableview 中显示自定义 UIMenuController ,就像这样

I am displaying custom UIMenuController in my tableview like this

但是问题是它在中心显示我想在标签的顶部显示它是橙色的。为了显示在标签之上,我在视图中执行了这个 [menu setTargetRect:CGRectMake(10,10,0,0):self.lbl]; 下面是整个代码。

but issue is that it is displaying in the center I want to display it on top of label which is orange colored. For displaying on top of label I did this [menu setTargetRect:CGRectMake(10, 10, 0, 0) inView:self.lbl]; below is the entire code.

但如果我显示 UIMenuController 而不是 UITableView setTargetRect 工作正常。

But if I am displaying the UIMenuController without UITableView setTargetRect works fine.

为什么 setTargetRect 无法使用 UITableView

setTargetRect Doc说:


(a)此目标矩形(targetRect)通常是选区的边界矩形
。 UIMenuController将编辑菜单定位在
矩形的上方;如果那里的菜单空间不足,那么
将它放在矩形下面。菜单的指针根据需要放置在目标矩形顶部或底部的
中心。

(a) This target rectangle (targetRect) is usually the bounding rectangle of a selection. UIMenuController positions the editing menu above this rectangle; if there is not enough space for the menu there, it positions it below the rectangle. The menu’s pointer is placed at the center of the top or bottom of the target rectangle as appropriate.

(b)注意,如果你制作宽度或目标矩形的高度
零,UIMenuController将目标区域视为
定位的线或点(例如,插入符号或单点)。

(b) Note that if you make the width or height of the target rectangle zero, UIMenuController treats the target area as a line or point for positioning (for example, an insertion caret or a single point).

(c)设置后,目标矩形不会跟踪视图;如果
视图移动(例如在滚动视图中发生),则必须相应地更新
目标矩形。

(c) Once it is set, the target rectangle does not track the view; if the view moves (such as would happen in a scroll view), you must update the target rectangle accordingly.

我缺少什么?

MyViewController

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TheCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cid" forIndexPath:indexPath];

    cell.lbl.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row];

    return 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 {
    // required
}

MyCustomCell

- (void)awakeFromNib {
    // Initialization code

    UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems: @[testMenuItem]];

    [menu setTargetRect:CGRectMake(10, 10, 0, 0) inView:self.lbl];

    [menu update];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:) || action == @selector(test:));
}

/// this methods will be called for the cell menu items
-(void) test: (id) sender {
    NSLog(@"test");
}

-(void) copy:(id)sender {
    UIMenuController *m = sender;
    NSLog(@"copy");
}


推荐答案

1)首先,请做这在你的视图控制器的 viewDidLoad 方法中。

1) First, please do this in your view controller's viewDidLoad method.

 UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test"
    action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];
 [[UIMenuController sharedMenuController] update];

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide:) name:UIMenuControllerWillHideMenuNotification object:nil];

2)然后,添加这两个方法。并将 menuFrame inView 设置为您的单元格

2) Then, add these two Methods. and set menuFrame inView to your cell

-(void)menuControllerWillShow:(NSNotification *)notification{
 //Remove Will Show Notification to prevent
 // "menuControllerWillShow" function to be called multiple times
 [[NSNotificationCenter defaultCenter]removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil];

//Hide the Original Menu View
UIMenuController* menuController = [UIMenuController sharedMenuController];
CGSize size = menuController.menuFrame.size;
CGRect menuFrame;
menuFrame.origin.x = self.tableView.frame.origin.x;
menuFrame.size = size;
[menuController setMenuVisible:NO animated:NO];

//Modify its target rect and show it again to prevent glitchy appearance
   [menuController setTargetRect:menuFrame inView:cell];
    [menuController setMenuVisible:YES animated:YES];
}

 -(void)menuControllerWillHide:(NSNotification *)notification{
    //re-register menuControllerWillShow
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:)
 name:UIMenuControllerWillShowMenuNotification object:nil];
 }

3)实现tableView委托并实现这些方法。

3) Implement tableView Delegates and implement these methods.

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell = (TableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    return YES;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    // required
}

4)使用

 -(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:) || action == @selector(test:)); } /// this methods will be called for the cell menu items
 -(void) test: (id) sender {
     NSLog(@"test"); }

 -(void) copy:(id)sender {
   NSLog(@"copy"); }

这篇关于UIMenuController方法setTargetRect:inView:在UITableView中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆