user2870930
user2870930

Reputation: 41

UIButton in UITableViewCell Not Working in iOS7

I have a UIButton in a UITableViewCell that has been working correctly since iOS4, now since the iOS7 update it no longer works. It is basically an image of an empty box. When user clicks on the image (UIButton) the image changes to a checked box. I am NOT using an XIB. Anybody have any suggestions? Thanks in advance.

(I have already tried contentView.userInteractionEnabled = NO; and [cell bringSubviewToFront:button] but this didn't work)

Here is some relevant code:

- (UITableViewCell *)taskCell:(NSIndexPath *)indexPath table:(UITableView *)localTableView managed:(NSManagedObject *)managedTask dateFormat:(NSDateFormatter *)localDateFormatter{
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [localTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        // Checkbox
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(-4.0f, -3.0f, 48.0f, 48.0f)];
        button.tag=kCellButtonViewTag;
        button.adjustsImageWhenHighlighted=NO;
        [button setImage:[UIImage imageNamed:@"uncheckedPriorityNone.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        [button release];
        }
}


- (IBAction)toggleCheckedMode:(id)sender{

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:clickedCell];
    Task *localTask = [self.fetchedResultsController objectAtIndexPath:indexPath];

    UIButton *button = sender;
    [button setImage:[UIImage imageNamed:@"checkedGray.png"] forState:UIControlStateNormal];
}

Upvotes: 4

Views: 7133

Answers (5)

averydev
averydev

Reputation: 5727

I was having trouble with this, and IB doesn't seem to let you place views outside of the contentview anymore. The way I handled it while still using IB for layout and what not is like this:

-(void)awakeFromNib{
    [self addSubview:self.myButton];
}

Upvotes: 3

HansPinckaers
HansPinckaers

Reputation: 1745

Try adding the view to the cell itself, not to the contentView.

So

[cell addSubview:button];

Instead of

[cell.contentView addSubview:button];

Upvotes: 5

Dhanas Manian
Dhanas Manian

Reputation: 516

If you want setEditing:YES/NO in your UITableView. You have to use below one, because hierarchy of the tableview is changed on ios7.

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)

{          
  [(UITableView *)self.superview isEditing]    
}
    else    

{    
        [(UITableView *)self.superview.superview isEditing]    
}

Upvotes: 0

jrturton
jrturton

Reputation: 119242

 UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];

This is your problem. The view hierarchy of cells has changed in iOS7, and you've been caught out by depending on this. My solution here still works in iOS7, I suggest you use that instead of tags or view hierarchy navigation.

Upvotes: 2

XF01
XF01

Reputation: 133

I found the same problem. The cleanest solution I could came up with that made support possible for pre-iOS7 and iOS7 was using the editingAccessoryView property from the UITableViewCell as well as the custom UIButton.

In my case I'm using a NIB, but that shouldn't make any difference. So in your case would be:

    // Checkbox
    if (([[[UIDevice currentDevice] systemVersion] compare:(@"7.0") options:NSNumericSearch] != NSOrderedAscending)) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(-4.0f, -3.0f, 48.0f, 48.0f);

        self.editingAccessoryView = button;

        [self.editingAccessoryView setHidden:YES];
    }
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(-4.0f, -3.0f, 48.0f, 48.0f)];
    button.tag=kCellButtonViewTag;
    button.adjustsImageWhenHighlighted=NO;
    [button setImage:[UIImage imageNamed:@"uncheckedPriorityNone.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];
    [button release];

The point here is that an action is still added for the custom UIButton, but on iOS7, another button is created with the same size, set to the editingAccessoryView and hidden. That should make the trick.

Remember to setEditing:YES in your UITableView.

Upvotes: 0

Related Questions