重用单元格时,UIMenuController会崩溃应用程序

时间:2014-03-14 11:31:17

标签: iphone objective-c uitableview ios7 uimenucontroller

我是使用UIMenuController的新手。我在UITableViewCell的长按手势上制作了一个UIMenuController。它运行良好,直到细胞不被重复使用。当我滚动表格视图并重复使用单元格时,长按单元格会导致应用程序崩溃。错误显示在行 - [cell becomeFirstResponder]。

我的代码是 -

- (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");
   }

我暂时陷入了这个问题。请帮忙。

UIBubbleTableViewCell内的代码是 - >

@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

1 个答案:

答案 0 :(得分:2)

请改为尝试:

- (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;
}

我不知道这是否会导致您的问题,但您只需要将手势识别器添加到每个单元格一次。此外,您应该在您的单元子类中使用initWithStyle: