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.