Dale Townsend
Dale Townsend

Reputation: 691

Show a table view footer on only one section

I am developing an app that lists products. The products are section headers. If you tap on a product it expands to show that sections' table, with modifications for that product in cells of that table.

What I want to do is have a footer for each section that will have an 'Add to Cart' button. That footer should only be visible when the product is expanded. If I use this:

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
    if (self.isOpen) {
        return 35.0;
    }
    else {
        return 0.0;
    }
} 

The footer doesn't show on any product section until you select a product, then it is visible on all products. Is there a way to have it a certain height on only one table section? I have tried detecting the last cell in the table with the modifications, but it turned out to be really buggy. The Add to Cart button would show on random rows.

Edit: Here is the cellForRow method to detect the last row:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ... Custom Cell dequeue

    // Currently only 3 rows for modifications, so 0-2, last one is 3 for Add to Cart
        if(indexPath.row == 3){
        cell.textLabel.text = @"Add to Cart";
        cell.productName.text = @"";
        cell.productDescription.text = @"";
    }
    else {
        ...Regular cell items
    }
    return cell;
}

Upvotes: 0

Views: 2699

Answers (1)

Lytic
Lytic

Reputation: 806

Are you also returning nil for your viewForFooter method in the other sections?

We will likely need to know how you check if a section has an addToCart button... but here's my guess:

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
    if (self.isOpen && [self hasCartForSection:section]) {
        return 35.0;
    }
    else {
        // One table view style will not allow 0 value for some reason
        return 0.00001;
    }
} 

- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section {
    if (self.isOpen && [self hasCartForSection:section]) {
       UIView *footerView = [UIView.alloc init];

       // Build your footer

       return footerView;
    }

    return nil;
} 

- (BOOL)hasCartForSection:(int)section {
   BOOL someCondition = NO;

   // Do your check

   if (someCondition) {
      return YES;
   }

   return NO;
}

Upvotes: 2

Related Questions