简体   繁体   中英

Adjusting the height of a uitableview section footer

I am just starting to use a UITableView section footer, but am having some problems. So I have a UITableView with an edit button attached to the bottom using a footer. My first problem is that when you click on that button, the footer should double in size because the edit button then becomes an add button and a done button, instead of just one button. What would be the best way to make the size of the footer bigger. I have tried changing the frame of the footer through self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88); but this does not work.

My second problem is that I would like the footer to always scroll with the table, like it is just another cell. It should just be located at the bottom of the table no matter what. If the user scrolls down, it should not stop at the bottom of the screen and stick, it should just keep scrolling with the rest of tableview. How would I fix this?

Here is how I am creating the footer:

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    //view.backgroundColor = [UIColor grayColor];

    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addButton.frame = CGRectMake(0, 0, 320, 44);
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateHighlighted];
    [self.addButton addTarget:self action:@selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
    self.addButton.hidden = YES;
    [view addSubview:self.addButton];

    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editButton.frame = CGRectMake(0, 0, 320, 44);
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editButton addTarget:self action:@selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.editButton];

    self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editDoneButton addTarget:self action:@selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
    self.editDoneButton.hidden = YES;
    [view addSubview:self.editDoneButton];

    self.editLabel = [[UILabel alloc] init];
    self.editLabel.frame = CGRectMake(10, 5, 100, 30);
    self.editLabel.text = @"Edit";
    self.editLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    [view addSubview:self.editLabel];

    self.addLabel = [[UILabel alloc] init];
    self.addLabel.frame = CGRectMake(10, 5, 100, 30);
    self.addLabel.text = @"Add";
    self.addLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.addLabel.hidden = YES;
    [view addSubview:self.addLabel];

    self.editDoneLabel = [[UILabel alloc] init];
    self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
    self.editDoneLabel.text = @"Done";
    self.editDoneLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.editDoneLabel.hidden = YES;
    [view addSubview:self.editDoneLabel];

    self.theLine = [[UIView alloc] init];
    self.theLine.frame = CGRectMake(0, 0, 320, .5);
    UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
    self.theLine.backgroundColor = borderColor;
    [view addSubview:self.theLine];

    return view;
}

Edit

After the taking into account the answers bellow, this is what I have switched to, but now where my footer used to be, there is just a grey rectangle.

Here is the code that I am now using:

-(void)setUpFooter {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
    view.backgroundColor = [UIColor redColor];

    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addButton.frame = CGRectMake(0, 0, 320, 44);
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateHighlighted];
    [self.addButton addTarget:self action:@selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
    self.addButton.hidden = YES;
    [view addSubview:self.addButton];

    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editButton.frame = CGRectMake(0, 0, 320, 44);
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editButton addTarget:self action:@selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.editButton];

    self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editDoneButton addTarget:self action:@selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
    self.editDoneButton.hidden = YES;
    [view addSubview:self.editDoneButton];

    self.editLabel = [[UILabel alloc] init];
    self.editLabel.frame = CGRectMake(10, 5, 100, 30);
    self.editLabel.text = @"Edit";
    self.editLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    [view addSubview:self.editLabel];

    self.addLabel = [[UILabel alloc] init];
    self.addLabel.frame = CGRectMake(10, 5, 100, 30);
    self.addLabel.text = @"Add";
    self.addLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.addLabel.hidden = YES;
    [view addSubview:self.addLabel];

    self.editDoneLabel = [[UILabel alloc] init];
    self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
    self.editDoneLabel.text = @"Done";
    self.editDoneLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.editDoneLabel.hidden = YES;
    [view addSubview:self.editDoneLabel];

    self.theLine = [[UIView alloc] init];
    self.theLine.frame = CGRectMake(0, 0, 320, .5);
    UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
    self.theLine.backgroundColor = borderColor;
    [view addSubview:self.theLine];

    view.userInteractionEnabled = YES;
    self.classTableView.tableFooterView = view;
    self.classTableView.tableFooterView.userInteractionEnabled = YES;

}

hope I can help.

To set the section footer height you also have to change the returned value in the method heightForFooterInSection . To reload the height you can call [tableView reloadData] or [tableView reloadSections:...] .

A table section footer always stick to the bottom of the screen but there is a different view used to place things after the table view. The tableView.tableFooterView is a view that is located after the table view. Change this to add elements after the table view.

Cheers!

There are two types of footers in a UITableView . There is the table footer at the bottom of the table, and there is the table section footer at the bottom of each section.

It sounds like you want to use table section headers. However, your code snippet is setting the frame of the table footer, not the table section footers.

self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88); 

If you want to adjust the table section footer height, the tableView:heightForFooterInSection: method will need to return a different value.

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