marzapower
marzapower

Reputation: 5611

Update section footer title in UITableView without reloading

I would like to be able to programmatically update the footer title of a section inside a table view, while I am typing text through a keyboard. The keyboard appears when I click on a cell to make its detailedText view editable, so I would like to update the footer title without reloading from the data source.

In fact, if I did this, the keyboard would disappear so it is not a good form of interaction. I've not been able to find a good solution to this problem... any suggestions?

Thank you

Upvotes: 7

Views: 8944

Answers (5)

thecoolwinter
thecoolwinter

Reputation: 1010

Swift 5 Answer

tableView.beginUpdates()

tableView.footerView(forSection: 0)?.textLabel?.text = "Text Here"

tableView.endUpdates()

Upvotes: 2

Mark Alldritt
Mark Alldritt

Reputation: 697

I know I'm late to this thread, but I've found you can simply do this:

[self.tableView beginUpdates];
[self.tableView footerViewForSection:section].textLabel.text = @"Whatever";
[[self.tableView footerViewForSection:section].textLabel sizeToFit];
[self.tableView endUpdates];

Upvotes: 11

Eugene Martinson
Eugene Martinson

Reputation: 99

Mark Alldritt's answer was good, but sometimes it doesn't size the label correctly. Fixed example is below (for section == 0):

[self.tableView beginUpdates];
UILabel *footerLabel = [self.tableView footerViewForSection:0].textLabel;

footerLabel.text = [self tableView:self.tableView titleForFooterInSection:0];
CGRect footerLabelFrame = footerLabel.frame;
footerLabelFrame.size.width = self.tableView.bounds.size.width;
footerLabel.frame = footerLabelFrame;
[self.tableView endUpdates];

Upvotes: 3

dip
dip

Reputation: 139

For first section (ziro)

[self.tableView footerViewForSection:0].textLabel.text = @"test";

Upvotes: 2

akashivskyy
akashivskyy

Reputation: 45180

If you have groupped table, you can use:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 17)]; //I'm not sure about the frame...
    yourLabel.font = [UIFont systemFontOfSize:16];
    yourLabel.shadowColor = [UIColor whiteColor];
    yourLabel.shadowOffset = CGSizeMake(0, 1);
    yourLabel.textAlignment = UITextAlignmentCenter;
    yourLabel.textColor = RGB(76, 86, 108);
    yourLabel.backgroundColor = [UIColor clearColor];
    yourLabel.opaque = NO;
    return yourLabel;
}

Declare yourLabel in your .h file. Then, you can access it via

yourLabel.text = @"whatever you want";

Please check if that works :)

Upvotes: 6

Related Questions