views:

170

answers:

3

I created a UITableViewCell in Interface Builder which I have added a UIImageView and a UIButton to. I have given the UIButton an outlet, and hooked it up to a IBAction with the touch up inside event set. I assumed it would call this method, as everything looked like it is hooked up:

- (IBAction)pressedCheckbox:(id)sender {
    [sender setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    NSLog(@"clicked check");

}

Checking the connections tab, I have everything set correctly, the outlet and the action. And I get no errors, but when previewing the app, clicking on that button inside the UITableViewCell does nothing, and I don't see anything in the logs.

Ideas on why it won't let me click my button which is in a UITableViewCell?

EDIT:

Code for cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                cell = topCell;
                break;
            case 1: 
                cell = cell1;
                break;
        }//end switch

    }//end if

    return cell;
}
A: 

Are you doing this in IB? It may be worth trying - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents (in the loadView method of the owning view controller) to see if the problem is the IB link.

Adam Eberbach
Yes, I am doing this in IB.
Nic Hubbard
No, using: [cell1Check addTarget:self action:@selector(pressedCheckbox:) forControlEvents:UIControlEventTouchUpInside]; did not work either.
Nic Hubbard
A: 

You mean that this is a subclass of UITableViewCell right? Are you sure that you are using this instead of the normal UITableViewCell?

Go to your cellForRowAtIndexPath method implementation and look for the line where you instantiate the cell. Make sure it's instantiating it to whatever your subclass is called, and not UITableViewCell. If you're confused, post the code for your codeForRowAtIndexPath method so we can see it.

Jorge Israel Peña
I posted the code. And no, it is not a subclass, just the normal UITableViewCell.
Nic Hubbard
Oh okay, so how did you add the UIButton to the cell?
Jorge Israel Peña
If you are doing this programatically, you might want to check this out: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW15
Jorge Israel Peña
I am not doing it programmatically, only because there are quite a few different UI elements in my cell, and I wanted to lay them out in IB. So, I just created a UITableViewCell in IB, then created an outlet for that, and called that outlet for the cell that I wanted to use it for.
Nic Hubbard
Look at this link which goes over creating a custom NIB and loading it: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW31
Jorge Israel Peña
I figured out the issue. and posted it as an answer.
Nic Hubbard
A: 

I also figured out what the problem was. I was needing to set different heights for cells and was using:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

     if (indexPath.section == 0) {

          switch (indexPath.row) {

               case 0:

                    return 26;

                    break;

          }//end switch

     }//end if

     return 44; <-- I just added this and it fixed the issue

}

Previously I was only returning a value for cell 0, so it was causing issues with all my other cells

Nic Hubbard
Don't return "44" - instead return tableView.rowHeight. It is probably 44 but you never know if it might change in some future iOS. Same thing for "26" - get something's frame.size.height if necessary.
Adam Eberbach
Thanks for the tip, I will change this.
Nic Hubbard