UITableView footerView with button, button doesn&#

2019-02-11 12:00发布

问题:

I'm trying to create a custom view for my UITableView footerView that has a button in it. I can see it there, but when I touch it, nothing happens... can you see what's wrong here:

- (void)viewDidLoad {
    [super viewDidLoad];

    if(self.tableView.tableFooterView == nil) {
        //allocate the view if it doesn't exist yet
        UIView *footerView  = [[UIView alloc] init];

        //create the button
        UIButton *addNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];     
        //the button should be as big as a table view cell
        [addNewBtn setFrame:CGRectMake(0, 0, 320, [AddMerchantTVC cellHeight])];

        //set title, font size and font color
        [addNewBtn setTitle:@"Add New" forState:UIControlStateNormal];
        [addNewBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [addNewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [addNewBtn addTarget:self action:@selector(addNew:) forControlEvents:UIControlEventTouchUpInside];

        UIImageView *cellGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellGradient.png"]];
        cellGradient.frame = CGRectMake(0, 54, 320, 16);
        [footerView addSubview:cellGradient];

        //add the button to the view
        [footerView addSubview:addNewBtn];
        footerView.userInteractionEnabled = YES;
        self.tableView.tableFooterView = footerView;
        self.tableView.tableFooterView.userInteractionEnabled = YES;

        [footerView release];
        [cellGradient release];
}

- (void)addNew:(id)sender {
    // This is never called
    NSLog(@"Touched.");
}

回答1:

You're not setting the footerView's frame. Set the frame of the footerView to be at least as high as the button otherwise the touches don't get passed down to the button.



回答2:

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

put this method in your class your problem will solve.....



回答3:

its worked for me i just add

footerView.frame =CGRectMake(0, 300, 100, 100);


回答4:

You need to set the tableView's sectionFooterHeight property before the call to viewForFooterInSection occurs (can't do it inside the viewForFooterInSection method).



回答5:

Create a subclass of UIView and include these methods:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return [super hitTest:point withEvent:event];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return [super pointInside:point withEvent:event];
}