UIButton in UITableViewCell not working in iOS7

I have a UIButton in a UITableViewCell that works correctly with iOS4, now since the iOS7 update no longer works. This is basically an image of an empty box. When the user clicks on the image (UIButton), the image changes to the marked field. I DO NOT use XIB. Anyone have any suggestions? Thanks in advance.

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

Here is the 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];
}
+4
source share
5 answers

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

So

[cell addSubview:button];

Instead

[cell.contentView addSubview:button];
+5
source

, IB, , . , , IB , :

-(void)awakeFromNib{
    [self addSubview:self.myButton];
}
+3
 UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];

. iOS7, . iOS7, .

+2

. , , pre-iOS7, iOS7 editAccessoryView UITableViewCell, UIButton.

NIB, . :

    // 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 the action is still added for the custom UIButton, but on iOS7 another button is created with the same size set in editAccessoryView and hidden. That should do the trick.

Remember setEditing: YES in your UITableView.

0
source

If you want setEditing: YES / NO in your UITableView. You should use below units because the tableview hierarchy changes to ios7.

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)

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

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

All Articles