简体   繁体   中英

Add a button on the custom UITableViewHeaderFooterView

I am trying to add a button programmatically on the custom UITableViewHeaderFooterView class as follows, it does not appear.

I wonder what I am missing or doing wrong ?

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {

        UIButton * clickBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.bounds.size.width-50, 0, 50, 50)];
        clickBtn.imageView.image = [UIImage imageNamed:@"carat-open.png"];
        [self.contentView addSubview:clickBtn];
    }
    return self;
}

Create UIButton inside of this function and add button as a subview to header view

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

     UIButton * clickBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.bounds.size.width-50, 0, 50, 50)];
     clickBtn.imageView.image = [UIImage imageNamed:@"carat-open.png"];

    UIView *headerView = [[UIView alloc] init];
    [headerView addSubview:clickBtn];
    headerView.backgroundColor=[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
    return headerView;
}

To add custom view in Section Footer:

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


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return  [self getViewForFooterForSection];
}


- (UIView*)getViewForFooterForSection
{
    UIView *sectionFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    sectionFooterView.backgroundColor = [UIColor redColor];
    return sectionFooterView;
}

To add custom view for section footer from custom view file

CustomFooter

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if(self)
    {
        self.frame = frame;
        self.backgroundColor = [UIColor blueColor];
    }

    return  self;
}

View Controller

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return  [self getViewForFooterForSection];
}


- (UIView*)getViewForFooterForSection
{
    CustomFooter *sectionFooterView = [[CustomFooter alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    return sectionFooterView;
}

To add custom view in Table footer View

tbl.tableFooterView = [self getViewForTableView];

- (UIView*)getViewForTableView
{
    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    tableFooterView.backgroundColor = [UIColor greenColor];
    return tableFooterView;
}

you can't set a button image like that .

clickBtn.imageView.image = [UIImage imageNamed:@"carat-open.png"];

imageView property is readonly.

use this one

  • (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM