views:

69

answers:

2

I'm setting up two buttons inside UITableViewCells. The cell itself shouldn't ever respond to selection, just my two buttons.

Here's the code in question:

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

Literally EVERYTHING about this works great, except that tapping either button does NOT result in my NSLog indicating we've gotten to the methods we're targeting. No errors either, just no message.

My stretchy images showing up tell me my IB connections are fine, in my custom table cell's nib.

It's almost like another view is over top of them so they're not receiving the click. But they're definitely the last things added to the view by my xib. No question about that.

EDIT: THE HACKING CONTINUES!!

I've just added the following code to my UITableViewCell subclass:

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

Hilariously enough, this works. But it's just bizarre that I'm having to reimplement button-contact-detection code like this. Still no idea why my buttons aren't receiving the tap. I'm not like wildly satisfied with this solution. I guess I'm willing to go into production with it if the real issue can't be found, but... I'm not asking for anything a hundred iPhone devs haven't done before me here, I don't think.

A: 

No idea if this will help a great deal (I'm not on my mac) but could you try calling bringSubviewToFront on each button to ensure they are in front of the viewing hierarchy?

davbryn
No help. I was pretty sure they were already in front... Now, I called that on the cell.contentView, which is the parent view of my buttons. Should I call that from further up the hierarchy, you think?
Dan Ray
i would try `[MyCell.view bringSubviewToFront:button1];`
Jesse Naugher
@Jesse: Tried it. I get "Request for member view in something not a structure or union". UITableViewCell names its main view property `.contentView`, not `.view`. Seems inconsistent, but I guess that's because it's a UIView subclass and not a UIViewController subclass.
Dan Ray
yes, i recall that now. i had this problem for a bit a while back, but got it working with a nib and adding a tag. Just make 200% sure your outlets and actions are hooked up correctly again, and make sure you send the buttons to the front through format in IB.
Jesse Naugher
+1  A: 

Interesting problem. Just to see if the cell is capturing the clicks go ahead and add an NSLog to your didselectrowatindexpath method.

E. Criss
Yep, that responds. Including when I click right on the buttons. (I'm in the Simulator at the moment, so I can be pretty precise with my clicking.) I'd have thought they'd capture the click and not pass it on to the cell....
Dan Ray
You've nudged me down... well maybe not the RIGHT track, but A track, for sure. See my edit to the OP question.
Dan Ray