尽管有所有方法,但 UITableView 上的 UIMenuController 仍不显示
Posted
技术标签:
【中文标题】尽管有所有方法,但 UITableView 上的 UIMenuController 仍不显示【英文标题】:UIMenuController On UITableView Does Not Show Up In Spite Of All Methods 【发布时间】:2013-10-16 11:51:22 【问题描述】:我有UITableView
来显示一些列表。实现UILongPressGestureRecognizer
来接听电话,我想在此显示删除、上传等操作的菜单。
接下来是实现
// Registering for long press event
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
lpgr.delegate = self;
[self.myTable addGestureRecognizer:lpgr];
长按我的控制功能开始发挥作用
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
CGPoint p = [gestureRecognizer locationInView:self.playbackTable];
NSIndexPath *indexPath = [self.playbackTable indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@"long press on table view but not on a row");
else
NSLog(@"long press on table view at section %d row %d", indexPath.section, indexPath.row);
CGPoint p = [gestureRecognizer locationInView: self.myTable];
NSIndexPath *indexPath = [self.myTable indexPathForRowAtPoint:p];
if (indexPath != nil)
if([self becomeFirstResponder])
UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteFileListItem:)];
menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:delete, nil]];
[menu setTargetRect:[self.myTable rectForRowAtIndexPath:indexPath] inView:self.myTable];
[menu setMenuVisible:YES animated:YES];
我也实现了以下方法
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
if (action == @selector(deleteFileListItem:))
return YES;
return NO;
-(BOOL)canBecomeFirstResponder
return YES;
和
- (void)deleteFileListItem:(id)sender
// Will perform action here
如果有任何遗漏或我做错了,请告诉我。
【问题讨论】:
【参考方案1】:将长按手势识别器附加到每个单元格而不是整个表格视图时,我已经成功了。我的猜测是这就是这里的问题。
【讨论】:
以上是关于尽管有所有方法,但 UITableView 上的 UIMenuController 仍不显示的主要内容,如果未能解决你的问题,请参考以下文章
Python分析数据难吗?某科技大学教授说,很难但有方法就简单
尽管 cellForRowAtIndexPath 返回有效单元格,但 UITableView 崩溃