views:

1351

answers:

1

I've successfully added some UIButtons to a custom tableFooterView. The problem is that the button events does not get called. Also, the image relative to the UIControlStateHighlighted doesn't come up. Any ideas? I've tried all I could think of, but I can't find the trick. I'm reporting here the interesting part of the UITableViewController:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section 
{
    CGRect footerFrame = CGRectMake(0.0, 0.0, 480, 190);
    _footerView = [[UIImageView alloc] initWithFrame: footerFrame];
    _footerView.image = [UIImage imageNamed:@"tableviewfooter.png"];
    _footerView.contentMode = UIViewContentModeTopLeft;

    UIButton *addExam = [self createButtonWithFrame: CGRectMake(10, 30, 460, 58) Target: self Selector: @selector(addNewItem:) Image: @"additem.png" ImagePressed: @"additem_pressed.png"];
    [_footerView addSubview:addExam];

    return _footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
    return 190;
}


- (void) addNewItem:(id)sender
{
    // This get's never called
    NSLog(@"Pressed");
}


- (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

    UIImage *newImage = [UIImage imageNamed: image];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

    UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    return button;
}

Thanks for your help!

+1  A: 

I do not think UIImageView was designed to work well as a container for other views, especially UIButtons. For one, UIImageViews, when created, have their userInteractionsEnabled property set to false by default.

I would just create a simple UIView as your footer view and then add both the UIButton and the UIImageView directly to it.

Sebastian Celis
Yep. That was the cause. I've learned something today :)
Stefano Verna