小编leb*_*aft的帖子

如何为UITableViewCell显示自定义UIMenuItem?

我希望当我长按UITableViewCell以显示自定义UIMenuItem时弹出的UIMenuController.

我在viewDidLoad中设置了自定义项

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];
Run Code Online (Sandbox Code Playgroud)

然后我设置所有正确的委托方法.

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return (action == @selector(copy:) || action == @selector(test:));
}

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    if (action == @selector(copy:)) {
         // do stuff
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

但它只是显示"复制"项目,因为我只允许它和我的自定义项目.但是,自定义项目不会显示.

我意识到,我可以为单元格本身添加一个手势识别器,但这种方式会破坏UIMenuController共享实例的目的,不是吗?

uitableview uimenucontroller ios

22
推荐指数
2
解决办法
2万
查看次数

Set UITableViewCell selected to YES in willDisplayCell: and the cell can't be deselected anymore

I want to pre-select some rows in a multiple selection UITableView.

I do this in tableView:willDisplayCell:forRowAtIndexPath: by simply setting [cell setSelected:YES animated:NO];.

However, for some reason this disables deselection for these rows. The embedded controls still work, and so do detail disclosure buttons.

I have uploaded a sample project here: https://github.com/leberwurstsaft/TableViewIssue where you can check out the behavior (in Viewcontroller.m lines 49-51).

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setSelected:NO animated:NO]; //   -->   setSelected:YES and the cells …
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview ios

3
推荐指数
2
解决办法
2435
查看次数

标签 统计

ios ×2

uitableview ×2

objective-c ×1

uimenucontroller ×1