我的按钮为什么不去?

时间:2010-07-12 20:27:23

标签: iphone uitableview interface-builder uibutton

我在UITableViewCells中设置了两个按钮。单元本身不应该响应选择,只需要我的两个按钮。

以下是相关代码:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"ResourceResultsCells";

    ResourceResultsTableCell *cell = (ResourceResultsTableCell *)[tableView dequeueReusableCellWithIdentifier:ident];

    if (!cell) {
        NSArray *ary = [[NSBundle mainBundle] loadNibNamed:@"ResourceResultsTableCell" owner:nil options:nil];
        for (id thing in ary) {
            if ([thing isKindOfClass:[ResourceResultsTableCell class]]) {
                cell = (ResourceResultsTableCell *)thing;
            }
        }

    }

    NSDictionary *listing = [self.listings objectAtIndex:indexPath.row];

    cell.crewName.text = [listing objectForKey:@"ListingTitle"];
    cell.cityState.text = [NSString stringWithFormat:@"%@, %@",
                           [listing objectForKey:@"City"],
                           [listing objectForKey:@"State"]];
    cell.phone.text = [listing objectForKey:@"phone1"];
    cell.email.text = [self safeListingOf:@"Email" from:listing];

    UIImage *stretchy = [[UIImage imageNamed:@"grey_tab_stretchable.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0];
    [cell.callButton setBackgroundImage:stretchy forState:UIControlStateNormal];
    [cell.addToLightboxButton setBackgroundImage:stretchy forState:UIControlStateNormal];

    cell.callButton.tag = indexPath.row;
    cell.addToLightboxButton.tag = indexPath.row;

    //here's where my trouble is....
    [cell.callButton addTarget:self action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];
    [cell.addToLightboxButton addTarget:self action:@selector(addToLightbox:) forControlEvents:UIControlEventTouchUpInside];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;             
}

-(void)call:(id)sender
{
    UIButton *hit = (UIButton *)sender;
    NSLog(@"call with id %d", hit.tag);
}

-(void)addToLightbox:(id)sender
{
    UIButton *hit = (UIButton *)sender;
    NSLog(@"lightbox with id %d", hit.tag);
}

从字面上看,关于这一切的一切都很好,除了点击任一按钮不会导致我的NSLog表明我们已经达到了我们所针对的方法。也没有错误,只是没有消息。

我的弹性图像显示告诉我我的IB连接很好,在我的自定义表格单元格的笔尖中。

这几乎就像另一个视图在他们之上,所以他们没有收到点击。但它们绝对是我的xib添加到视图中的最后一件事。毫无疑问。

编辑:黑客继续!!

我刚刚将以下代码添加到我的UITableViewCell子类中:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    if ([touches count] == 1) {
        for (UITouch *theTap in touches) {
            if (theTap.tapCount == 1) {
                CGPoint coords = [theTap locationInView:self.contentView];

                CGRect callButtonFrame = self.callButton.frame;
                if (coords.x > callButtonFrame.origin.x && coords.x < (callButtonFrame.origin.x + callButtonFrame.size.width) 
                    && coords.y > callButtonFrame.origin.y && coords.y < (callButtonFrame.origin.y + callButtonFrame.size.height)) {
                    [self call:callButton];
                }

                CGRect boxButtonFrame = self.addToLightboxButton.frame;

                if (coords.x > boxButtonFrame.origin.x &&  coords.x < (boxButtonFrame.origin.x + boxButtonFrame.size.width)
                    && coords.y > boxButtonFrame.origin.y && coords.y < (boxButtonFrame.origin.y + boxButtonFrame.size.height)) {
                    [self addToLightbox:addToLightboxButton];
                }
            }
        }
    }
}

足够滑稽,这很有效。但是,我不得不重新实现像这样的按钮接触检测代码。仍然不知道为什么我的按钮没有接收到水龙头。我对此解决方案并不满意。如果找不到真正的问题,我想我愿意用它来投入生产,但是......我不是要求任何一百个iPhone开发者在我之前没有做过,我不认为

2 个答案:

答案 0 :(得分:1)

有趣的问题。只是为了查看单元格是否正在捕获点击,然后向您的didselectrowatindexpath方法添加NSLog。

答案 1 :(得分:0)

不知道这是否会有很大帮助(我不在我的Mac上)但是你可以尝试在每个按钮上调用bringSubviewToFront以确保它们位于查看层次结构的前面吗?