开发者

Putting a button in table footer view

开发者 https://www.devze.com 2023-03-20 15:09 出处:网络
I am creating a custom button and putting it in my table\'s footer view but the button is going way out from the right corner.

I am creating a custom button and putting it in my table's footer view but the button is going way out from the right corner.

What is wrong here!?!

Here is my code and output i开发者_StackOverflow中文版mage:

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
    [aButton setBackgroundImage:[[UIImage imageNamed:@"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
    [aButton setTitle:@"Click me" forState:UIControlStateNormal];
    [aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
    [aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
    [self.tableView setTableFooterView:aButton];

Putting a button in table footer view


I added the button in the UIView and then set the tableFooterView to this UIView object and it worked. We cannot directly put UIButton as tableFooterView.


Try doing like this. Set IBOutlet UIView *footerView; and synthesize it. Add it as a subview using the method-

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

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


    //create the button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setFrame:CGRectMake(10, 3, 300, 44)];

    //set title, font size and font color
    [button setTitle:@"Click Me" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];

    //set action of the button
    [button addTarget:self action:@selector(clickPressed:)
     forControlEvents:UIControlEventTouchUpInside];

    //add the button to the view
    [footerView addSubview:button];
}

//return the view for the footer
return footerView;
}


One important addition to Legolas' fine answer: You'll need to implement the tableView delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    foot 50;
}

This was driving me nuts because the button (or, in my case, buttons) were not responding to the touch event. Adding the delegate method made it all better. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消