在IOS应用程序中,当我长按位于UITableViewCell中的UIView时,我想显示一个UIMenuController。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
myTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil)
{
cell=(myTableViewCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
myView=<initMyView>
[cell.contentView addSubview:myView];
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCapturedOnView:)];
longTap.minimumPressDuration=0.5f;
[myView addGestureRecognizer:longTap];
return cell;
}
- (void)longTapGestureCapturedOnView:(UITapGestureRecognizer *)gesture
{
items=[[NSMutableArray alloc]init];
if (gesture.state == UIGestureRecognizerStateBegan)
{
[items addObject:[[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(myCopy:)]];
[items addObject:[[UIMenuItem alloc] initWithTitle:@"Custom" action:@selector(myCustom:)]];
}
[[UIMenuController sharedMenuController] setMenuItems:items];
[[UIMenuController sharedMenuController] update];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
BOOL result = NO;
if(@selector(myCopy:) == action ||@selector(myCustom:) == action ) {
result = YES;
}
return result;
}
调用longTapGestureCapturedOnView
,以及在两种情况下返回YES的canPerformAction
,但屏幕上没有出现菜单项。我能做错什么呢?
发布于 2018-06-02 05:16:37
您还需要
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES ;
}
//
此外,在cellForRowAt
中添加手势也是不正确的,因为它是在scroll & cellReusing上调用的,所以请将其添加到UITableViewCell
自定义类的awakeFormNib
方法中
https://stackoverflow.com/questions/50650727
复制相似问题