Hide footer view in UITableView

落花浮王杯 提交于 2019-11-28 10:47:52

There are four solutions. They are,

Solution 1:

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
    return 1.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
    return 1.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

Solution 2:

You can set the footer/header height via interface builder under the size tab.

Solution 3:

set contentInset property.

self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);

It is used to make the top and bottom touch the edge.

Solution 4:

implement the below, set the values as per your condition. 0.0 will not be accepted. The lower value should be 1.0.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
    if(section == 0) {
       return 6;
    } else {
       return 1.0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
    return 5.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

This should do it

tableView.tableFooterView.hidden = YES;

This can do it by implementing delegate method

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return CGFLOAT_MIN;
}

Referring to @J.Hong answer, Swift4 version (iOS 11.3):

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNonzeroMagnitude
}

Comparing to @Tim Newton answer:

  • there is no need to call titleForFooterInSection
  • simplify CGFloat.leastNormalMagnitude -> .leastNonzeroMagnitude (more Swifty IMO)

Swift 3.0 solution that also removes the pesky 1px border between the previous cell and the following header.

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return nil
}

in swift4:

self.tableView.tableFooterView = nil

When not using custom views, return nil from tableView:titleForFooterInSection: and 0 from tableView:heightForFooterInSection:.

@property(nonatomic, strong) NSString *footerTitle;
@property(nonatomic, assign) BOOL shouldHideFooter;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return self.shouldHideFooter ? nil : self.footerTitle;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.shouldHideFooter ? 0 : UITableViewAutomaticDimension;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!