Question

I am new in using UIMenuController . I have made a UIMenuController on long press gesture of UITableViewCell. Its running fine till cells are not reused. When i scroll the table view and cells are reused , long pressing on cell crashes application. The error is shown on line - [cell becomesFirstResponder].

My code is-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{    
    // Standard bubble

    NSBubbleData *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    static NSString *cellId1 = @"tblBubbleCell";
    UIBubbleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId1];
    if (cell == nil) cell = [[UIBubbleTableViewCell alloc] init];
    cell.data = data;
    cell.key=data.key;
    cell.showAvatar = self.showAvatars;

    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [cell addGestureRecognizer:recognizer];
    return cell;
}


#pragma mark - Menu controller

- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
       UIBubbleTableViewCell *referenceCell = (UIBubbleTableViewCell *)recognizer.view;
       [referenceCell becomeFirstResponder];//error line when cell is reused and long  pressed

   UIMenuItem *forward = [[UIMenuItem alloc] initWithTitle:@"Forward" action:@selector(forward:)];

    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObjects:forward, nil]];
    [menu setTargetRect:referenceCell.frame inView:referenceCell.superview];
    [menu setMenuVisible:YES animated:YES];
}
}

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

- (void)delete:(id)sender {

    }

- (void)forward:(id)sender {
    NSLog(@"Cell forwarded");
   }

I stuck in this problem from a while. Please help.

Code inside UIBubbleTableViewCell is->

@implementation UIBubbleTableViewCell

@synthesize data = _data;
@synthesize customView = _customView;
@synthesize bubbleImage = _bubbleImage;
@synthesize showAvatar = _showAvatar;
@synthesize avatarImage = _avatarImage;
@synthesize userName=_userName;
@synthesize key=_key;

- (BOOL)canBecomeFirstResponder {
   return YES;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
[self setupInternalData];
}

#if !__has_feature(objc_arc)
- (void) dealloc
{
  self.data = nil;
  self.customView = nil;
  self.bubbleImage = nil;
  self.avatarImage = nil;
  [super dealloc];
}
#endif

- (void)setDataInternal:(NSBubbleData *)value
{
self.data = value;
[self setupInternalData];
}

- (void) setupInternalData
{
   self.selectionStyle = UITableViewCellSelectionStyleNone;

   if (!self.bubbleImage)
   {
   #if !__has_feature(objc_arc)
    self.bubbleImage = [[[UIImageView alloc] init] autorelease];
   #else
    self.bubbleImage = [[UIImageView alloc] init];        
   #endif
    [self addSubview:self.bubbleImage];
   }

   NSBubbleType type = self.data.type;

   CGFloat width = self.data.view.frame.size.width;
   CGFloat height = self.data.view.frame.size.height;

   CGFloat x = (type == BubbleTypeSomeoneElse) ? 0 : self.frame.size.width - width -   self.data.insets.left - self.data.insets.right;
   CGFloat y = 0;

   // Adjusting the x coordinate for avatar
   if (self.showAvatar)
    {
    [self.avatarImage removeFromSuperview];
    #if !__has_feature(objc_arc)
    self.avatarImage = [[[UIImageView alloc] initWithImage:(self.data.avatar ?      self.data.avatar : [UIImage imageNamed:@"missingAvatar.png"])] autorelease];
    #else
    self.avatarImage = [[UIImageView alloc] initWithImage:(self.data.avatar ?   self.data.avatar : [UIImage imageNamed:@"missingAvatar.png"])];
    #endif
    self.avatarImage.layer.cornerRadius = 9.0;
    self.avatarImage.layer.masksToBounds = YES;
    self.avatarImage.layer.borderColor = [UIColor colorWithWhite:0.0 alpha:0.2].CGColor;
    self.avatarImage.layer.borderWidth = 1.0;

    CGFloat avatarX = (type == BubbleTypeSomeoneElse) ? 2 : self.frame.size.width - 52;
    CGFloat avatarY = self.frame.size.height - 50;

    self.avatarImage.frame = CGRectMake(avatarX, avatarY, 50, 50);
    [self addSubview:self.avatarImage];

    CGFloat delta = self.frame.size.height - (self.data.insets.top + self.data.insets.bottom + self.data.view.frame.size.height);
    if (delta > 0) y = delta;

    if (type == BubbleTypeSomeoneElse) x += 54;
    if (type == BubbleTypeMine) x -= 54;
}

[self.customView removeFromSuperview];
self.customView = self.data.view;
self.customView.frame = CGRectMake(x + self.data.insets.left, y +     self.data.insets.top, width, height);
[self.contentView addSubview:self.customView];

if (type == BubbleTypeSomeoneElse)
{
    self.bubbleImage.image = [[UIImage imageNamed:@"bubbleSomeone.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];

}
else {
    self.bubbleImage.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:14];
}
self.bubbleImage.frame = CGRectMake(x, y, width + self.data.insets.left +   self.data.insets.right, height + self.data.insets.top + self.data.insets.bottom);

}


@end
Was it helpful?

Solution

Try this instead:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{    
    // Standard bubble

    NSBubbleData *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    static NSString *cellId1 = @"tblBubbleCell";
    UIBubbleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId1];
    if (cell == nil) {
        cell = [[UIBubbleTableViewCell alloc] init];
        UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [cell addGestureRecognizer:recognizer];
    }

    cell.data = data;
    cell.key=data.key;
    cell.showAvatar = self.showAvatars;

    return cell;
}

I don't know if this is causing your problem, but you only need to add the gesture recognizer to each cell once. Also, you should probably use initWithStyle: in your cell subclass.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top