当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。

如何移除这些单元格?


当前回答

你可能会找到这个问题的很多答案。大多数都围绕着UITableView的tableFooterView属性进行操作这是隐藏空行的合适方法。为了方便起见,我创建了一个简单的扩展,它允许打开/关闭接口生成器中的空行。 你可以从这个要点文件里查到。我希望它能为你节省一点时间。

extension UITableView {

  @IBInspectable
  var isEmptyRowsHidden: Bool {
        get {
          return tableFooterView != nil
        }
        set {
          if newValue {
              tableFooterView = UIView(frame: .zero)
          } else {
              tableFooterView = nil
          }
       }
    }
}

用法:

tableView.isEmptyRowsHidden = true

其他回答

我很幸运地实现了一个被接受的答案(iOS 9+, Swift 2.2)。我尝试着执行:

self.tableView.tableFooterView = UIView(frame: .zero)

然而,对我的tableView没有任何影响-我相信这可能与我使用UITableViewController的事实有关。

相反,我只需要重写viewForFooterInSection方法(我没有在其他地方设置tableFooterView):

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView(frame: .zero)
}

这对于具有单个section的tableView来说效果很好(如果有多个section,则需要指定最后一个)。

如果您不希望在最后一个单元格之后有任何分隔符,那么您需要为页脚设置一个接近零但非零的高度。

在你的UITableViewDelegate中:

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

我知道这个问题已经被接受的答案,但我在这里放了不同的方法,如何隐藏额外的分隔线UITableView。

您可以隐藏tableView的标准分隔线,并在每个单元格的顶部添加您的自定义行。

更新:

添加自定义分隔符最简单的方法是添加高度为1px的简单UIView:

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor grayColor]; /// may be here is clearColor;
[cell.contentView addSubview:separatorLineView];

OR

    self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
    self.tblView.delegate=self;
    self.tblView.dataSource=self;
    [self.view addSubview:self.tblView];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.tblView setTableHeaderView:v];
    [self.tblView setTableFooterView:v];
    [v release];

OR

- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // This will create a "invisible" footer
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    // To "clear" the footer view
    return [[UIView new] autorelease];
}

或 我喜欢的最好最简单的方式就是

self.tableView.tableFooterView = [[UIView alloc] init];

试试其中任何一种;

Uitableview额外的分隔线隐藏额外的分隔线隐藏在swift 3.0

 self.tbltableView.tableFooterView = UIView(frame: .zero)

试试这个

self.tables.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];