William Entriken
William Entriken

Reputation: 39253

iOS, UITableView with custom footer text AND custom footerview

I've got a grouped table. I've got two rules for this table, under the first condition, I need to show a pretty picture in the footer (this works). In the second condition I need to show some text in the footer (this fails). Is it possible to use titleForFooterInSection and viewForFooterInSection on the same table? And if I use them on the same section at the same time, which one should have precedence?

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section < self.practiceLessonSet.lessons.count) {
        if ([[self.practiceLessonSet.lessons objectAtIndex:section] words].count == 1) {
            return @"No replies yet. Try the share button?";
        }
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (self.practiceLessonSet.lessons.count == 0) {
        UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"introPractice"]];
        view.contentMode = UIViewContentModeCenter;
        return view;
    } else
        return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.practiceLessonSet.lessons.count == 0)
        return 278;
    else
        // WHAT SHOULD I PUT HERE?
        return [super tableView:tableView heightForFooterInSection:section]; // <-- FAILS
}

Upvotes: 0

Views: 1081

Answers (3)

mixable
mixable

Reputation: 1158

Just return the value UITableViewAutomaticDimension in heightForFooterInSection

Objective-C:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger)section
{
    if (self.practiceLessonSet.lessons.count == 0)
        return 278;
    else
        return UITableViewAutomaticDimension;
}

Swift:

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
{
    if (self.practiceLessonSet.lessons.count == 0) {
        return 278
    }
    return UITableViewAutomaticDimension
}

Upvotes: 0

Sal Aldana
Sal Aldana

Reputation: 1235

As Kale said, yes you should be able to use them both so something is failing in your condition checking in either the Title or the View section that is causing the problem (try putting a couple Log statements and follow through it to see if they are actually getting called and where they are stopping). From my experience, if you have both the View and the Title for either the Footer or Header, the view ends up taking precedence if both have a value.

Upvotes: 0

Kale McNaney
Kale McNaney

Reputation: 457

Yes, you can use them both. My guess is that these conditions aren't being met when you expect them to be:

if (section < self.practiceLessonSet.lessons.count) {
    if ([[self.practiceLessonSet.lessons objectAtIndex:section] words].count == 1) {
        return @"No replies yet. Try the share button?";
    }
}

Upvotes: 1

Related Questions