Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 490
user2870930 Avatar asked Dec 26 '22 19:12

user2870930


1 Answers

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

So

[cell addSubview:button];

Instead of

[cell.contentView addSubview:button];
like image 63
HansPinckaers Avatar answered Dec 28 '22 08:12

HansPinckaers