仅在一个部分上显示表格视图页脚

时间:2015-03-28 02:29:42

标签: ios objective-c xcode uitableview

我正在开发一个列出产品的应用。产品是节标题。如果您点击某个产品,它会展开以显示该部分的表格,并在该表格的单元格中对该产品进行修改。

我想要做的是为每个具有“添加到购物车”按钮的部分设置页脚。只有在产品扩展时才能看到该页脚。如果我用这个:

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

在您选择产品之前,页脚不显示在任何产品部分上,然后在所有产品上都可以看到。有没有办法让它只在一个桌子部分有一定的高度?我已经尝试通过修改检测表中的最后一个单元格,但事实证明它确实是错误的。 “添加到购物车”按钮将显示在随机行上。

编辑:这是检测最后一行的cellForRow方法:

- (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;
}

1 个答案:

答案 0 :(得分:1)

你是否也在其他部分的viewForFooter方法中返回nil?

我们可能需要知道您如何检查某个部分是否有addToCart按钮......但这是我的猜测:

- (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;
}