daihovey
daihovey

Reputation: 3575

Align text in UITableView's Footer

I'm using

- (NSString *)tableView:(UITableView *)tv titleForFooterInSection:(NSInteger)section
{
if (section == 1)
{
    return @
    "Some text  \r\n"
    "\r\n"
    "1. Point 1\r\n "
    "2. Point 2\r\n"
    "3. Point 3";
}
    return @"";
}

for the footer of a group of cells. It works great, but I want to align the text to the left (to help the look). I know you can create a view / UILabel using viewForFooterInSection but it seems an ugly work around. Is there any way to align it?

Upvotes: 7

Views: 5668

Answers (3)

Elijah
Elijah

Reputation: 8610

override func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
    let footerView = view as! UITableViewHeaderFooterView        
    footerView.textLabel?.textAlignment = .center //or whatever you want
}

Upvotes: 0

Ardavan Kalhori
Ardavan Kalhori

Reputation: 1698

if your tableview is short, then you can also write something like this:

-(void)viewDidLayoutSubviews 
{
    for (int section = 0; section < [self.tableView numberOfSections]; section++)
    {
        [self.tableView footerViewForSection:section].textLabel.textAlignment = NSTextAlignmentRight;
    }
}

Upvotes: 4

amattn
amattn

Reputation: 10065

tableView:viewForFooterInSection: is not a workaround.

It's common for Apple to give you two options, the easy way and the manual way.

If the easy way (tableView:titleForFooterInSection:) isn't doing what you want, you have the ultimate in flexibility by adding your own view.

In fact, you can just return a UILabel in tableView:viewForFooterInSection:

Should be super easy.

Upvotes: 5

Related Questions