在UITableViewController的第一行中使用UITapGestureRecognizer的UIImage无法正常工作

时间:2012-03-19 20:34:08

标签: objective-c ios uiimage uitableview uitapgesturerecognizer

我有一个UITableViewController子类,由另外两个类子类化。在两个表的一些单元格中,我有一个用户可以单击以执行操作的图像。一切正常,我很高兴,除了在2个子类中的一个中,该表的第一行有问题。您只能通过单击图像的最右边缘来单击图像。如果单击图像的主体,则会调用表的didSelectRowAtIndexPath。

令我难以置信的是,它适用于所有行的其他类,甚至在它不起作用的那个行中也适用于该图像显示的所有其他行。这是我用来将图像和手势识别器添加到我的单元格中的代码:

        UIImageView *imgparent = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 24.0, 14.0, 16.0)];
        imgparent.image = [UIImage imageNamed:@"item_open.png"];
        imgparent.tag = ITEMOPENTAG;
        // add listener
        imgparent.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleFingerDTapParent = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadChildren:)];
        [imgparent addGestureRecognizer:singleFingerDTapParent];
        [singleFingerDTapParent release];
        [cell.contentView addSubview:imgparent];
        [imgparent release];

顺便说一下,我正在模拟器中进行测试。我尝试用按钮替换图像,但问题仍然存在。有任何想法吗?

编辑:以下是单击图像时调用的代码的开头部分。同样,这是有效的,但在这一行的情况下,它只有在你单击图像的右边缘时才有效。我附上了截图来说明。第一个圆圈表示我必须点击的位置才能调出我的手势。第二个显示了同一个表中的一个示例,它允许您单击整个图像。我很困惑。

- (void)loadChildren:(UIGestureRecognizer *)gestureRecognizer {

    NSLog(@"loadChildren");

    // get the cell
    UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view.superview.superview;
    UIImageView *imgvw = (UIImageView *)[cell viewWithTag:ITEMOPENTAG]; // open arrow
    [imgvw setHidden:YES]; // hide open arrow
    imgvw = (UIImageView *)[cell viewWithTag:ITEMCLOSETAG]; // close arrow
    [imgvw setHidden:NO]; // show close arrow

    // get the record for the cell
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}

enter image description here

编辑:以下是完整的tableView:cellForRowAtIndexPath:indexPath。请记住,它是从此类的其他2个子类的cellForRowAtIndexPath调用的,每个子类只传递一个标识符withType

- (UITableViewCell *)tableView:(UITableView *)tv
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                      withType:(NSString *)s_type
{

    UITableViewCell *cell = nil;

    // if dealing with empty region then show no tasks cell
    if(s_type == @"noTasksCell"){
        cell = [tv dequeueReusableCellWithIdentifier:@"noTasksCell"];
        if( cell == nil ) { 
            cell = [[[UITableViewCell alloc]
                     initWithStyle:UITableViewCellStyleDefault 
                     reuseIdentifier:@"noTasksCell"] autorelease];
        }           
        cell.textLabel.text = @"No tasks";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }

    UILabel *lblMain;
    NSDictionary *o_rec = [self getRecForPath:indexPath];
    NSString *s_cell = @"rowCell";
    BOOL b_parent = NO;
    BOOL b_parentOpen = NO;
    BOOL b_child = NO;
    BOOL b_checked = NO;
    if([self isParent:indexPath]){
        b_parent = YES;
        b_parentOpen = !([o_rec objectForKey:@"b_open"] == nil || [[o_rec objectForKey:@"b_open"] isEqualToNumber:[NSNumber numberWithInt:0]]);
        s_cell = [s_cell stringByAppendingString:@"Parent"];
    }
    if([o_rec objectForKey:@"b_child"] != nil){
        b_child = YES;
        s_cell = [s_cell stringByAppendingString:[NSString stringWithFormat:@"Child%@",[o_rec objectForKey:@"indent"]]];
    }
    if([[o_rec objectForKey:@"checked"] isEqualToNumber:[NSNumber numberWithInt:1]]){
        b_checked = YES;
        s_cell = [s_cell stringByAppendingString:@"isComplete"];
    }
    // add the following to the name:
    //  - project id
    //  - width of table to the name so that rotations will change the cell dequeue names
    // - priority
    s_cell = [s_cell stringByAppendingFormat:
              @"Proj%@Width%dP%@"
              ,[o_rec objectForKey:@"project_id"]
              ,(int)tv.bounds.size.width
              ,[[o_rec objectForKey:@"priority"] stringValue]
              ];

    cell = [tv dequeueReusableCellWithIdentifier:s_cell];
    if( cell == nil ) { 
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:s_cell] autorelease];
        cell.textLabel.hidden = YES; // hide the regular text label
        cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        cell.selectedBackgroundView.backgroundColor = [delegate colorForHexWithAlpha:0xffcc66ff];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        int offsetleftmain = 10;
        if(b_child){
            offsetleftmain += ([[o_rec objectForKey:@"indent"] intValue]-1) * 18;
        }
        if(b_parent){
            // parent arrow
            UIImageView *imgparent = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 24.0, 14.0, 16.0)];
            imgparent.image = [UIImage imageNamed:@"item_open.png"];
            imgparent.tag = ITEMOPENTAG;
            // add listener
            imgparent.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleFingerDTapParent = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadChildren:)];
            singleFingerDTapParent.numberOfTapsRequired = 1;
            singleFingerDTapParent.numberOfTouchesRequired = 1;
            [imgparent addGestureRecognizer:singleFingerDTapParent];
            [singleFingerDTapParent release];
            [cell.contentView addSubview:imgparent];
            [imgparent release];
            // close arrow
            UIImageView *imgparent2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain-2, 24.0, 16.0, 14.0)];
            imgparent2.image = [UIImage imageNamed:@"item_close.png"];
            imgparent2.tag = ITEMCLOSETAG;
            imgparent2.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleFingerDTapParent2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideChildren:)];
            [imgparent2 addGestureRecognizer:singleFingerDTapParent2];
            [singleFingerDTapParent2 release];
            [cell.contentView addSubview:imgparent2];
            [imgparent2 release];
        }
        offsetleftmain += 18;
        // checkbox
        UIImageView *imgchk = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 20.0, 24.0, 24.0)];
        imgchk.image = [UIImage imageNamed:@"check_empty.png"];
        imgchk.tag = EMPTYCHECKTAG;
        // add listener
        imgchk.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkOnItem:)];
        [imgchk addGestureRecognizer:singleFingerDTap];
        [singleFingerDTap release];
        [cell.contentView addSubview:imgchk];
        [imgchk release];
        // checked checkbox
        UIImageView *imgchk2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 20.0, 24.0, 24.0)];
        imgchk2.image = [UIImage imageNamed:@"check_checked.png"];
        imgchk2.tag = CHECKTAG;
        // add listener
        imgchk2.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleFingerDTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkOffItem:)];
        [imgchk2 addGestureRecognizer:singleFingerDTap2];
        [singleFingerDTap2 release];
        [cell.contentView addSubview:imgchk2];
        [imgchk2 release];
        offsetleftmain += 28;
        // main label
        lblMain = [[UILabel alloc] initWithFrame:CGRectMake(offsetleftmain, 22.0, tv.bounds.size.width-offsetleftmain-10, 30.0)];
        lblMain.tag = MAINLABELTAG;
        lblMain.numberOfLines = 4;
        lblMain.font = delegate.font_dflt;
        // change color based on priority
        if (5-[[o_rec objectForKey:@"priority"] intValue] == 1)
            lblMain.textColor = [UIColor redColor];
        else if (5-[[o_rec objectForKey:@"priority"] intValue] == 2)
            lblMain.textColor = [delegate colorForHexWithAlpha:H_P2COLOR];
        else if (5-[[o_rec objectForKey:@"priority"] intValue] == 3)
            lblMain.textColor = [delegate colorForHexWithAlpha:H_P3COLOR];

        [cell.contentView addSubview:lblMain];
        [lblMain release];

        // show action sheet for long press and hold
        UILongPressGestureRecognizer *clicknHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellClicknHold:)];
        [cell addGestureRecognizer:clicknHold];
        [clicknHold release];

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // task text
    lblMain = (UILabel *)[cell viewWithTag:MAINLABELTAG];
    lblMain.text = [self formatContent:[o_rec objectForKey:@"content"]];

    CGRect newFrame = lblMain.frame;
    newFrame.size.height = [[o_rec objectForKey:@"height"] floatValue];
    //newFrame.size.height = [[o_rec objectForKey:@"height"] floatValue]+12;
    lblMain.frame = newFrame;
    // set checked status
    [(UIImageView *)[cell viewWithTag:EMPTYCHECKTAG] 
     setHidden:[[o_rec objectForKey:@"checked"] boolValue]];
    [(UIImageView *)[cell viewWithTag:CHECKTAG] 
     setHidden:![[o_rec objectForKey:@"checked"] boolValue]];
    // show open arrow if dealing with parent cell
    if (b_parent) {
        //NSLog(@"b_parentOpen:%d",b_parentOpen);
        [(UIImageView *)[cell viewWithTag:ITEMOPENTAG] setHidden:b_parentOpen];
        [(UIImageView *)[cell viewWithTag:ITEMCLOSETAG] setHidden:!b_parentOpen];
    }

    return cell;
}

1 个答案:

答案 0 :(得分:0)

以防万一有人遇到类似的罕见问题,这可能会有助于避免某人秃顶拔头发。所以我的问题不在于tableView本身,而是我的侧边栏类似于Facebook侧边栏滑入和滑出。我的侧边栏中有一个tableview,并且该表上有一个标题,由于某种原因,当它在我的另一个表旁边时,某种程度上阻止了对第一行中图像的点击。我的解决方案只是将侧边栏视图远离其旁边的视图远离屏幕,而不是紧挨着它。问题解决了。感谢所有试图帮助我的人。