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

如何移除这些单元格?


当前回答

Swift 3 /Swift 4 /Swift 5 +,非常简单

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
      //MARK:- For Hiding the extra lines in table view.
    tableView?.tableFooterView = UIView()
}

OR

override func viewDidLoad(_ animated: Bool) {
    super.viewDidLoad(animated)
      //MARK:- For Hiding the extra lines in table view.
    tableView?.tableFooterView = UIView()
}

其他回答

这里有另一种不使用分组表样式的方法,一种您可能猜不到的方法。向表中添加页眉和页脚(也许一个或另一个就足够了,还没有选中)会导致分隔符从填充行/空白行中消失。

我无意中发现了这一点,因为我想在桌子的顶部和底部留出一点空间,以减少触碰按钮的风险,而不是手指肥大的表格单元格。下面是一个将空白视图作为页眉和页脚插入的方法。使用您喜欢的任何高度,您仍然可以消除额外的分隔线。

- (void) addHeaderAndFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.myTableView setTableHeaderView:v];
    [self.myTableView setTableFooterView:v];
    [v release];
}

In response to @Casebash, I went back to the code in my app ("AcmeLists" List Manager in iTunes store...) and short-circuited the addHeaderAndFooter method to verify. Without it, I have the extra row separators; with the code, I have what you see in this window snap: no table row separators picture. So I'm not sure why it wouldn't have worked for you. Moreover, it makes sense to me that having any custom footer on a table view would necessarily have to stop drawing row separators for blank rows below it. That would be hideous. For reference, I looked at tables where there were more rows than could be viewed on screen, and then for a table with two rows. In both cases, no extraneous separators.

也许您的自定义视图实际上并没有添加。要检查这一点,将背景颜色设置为clearColor以外的颜色,例如,[UIColor redColor]。如果您在表格底部没有看到一些红色条,则表示您的页脚没有设置。

试试这个。这招对我很管用:

- (void) viewDidLoad
{
  [super viewDidLoad];

  // Without ARC
  //self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];

  // With ARC, tried on Xcode 5
  self.tableView.tableFooterView = [UIView new];
}

我只是在ViewDidLoad函数中添加了这一行,问题就解决了。

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

如果你在你的视图中有一个搜索栏(例如限制结果的数量),你还必须在shouldReloadTableForSearchString和shouldReloadTableForSearchScope中添加以下内容:

controller.searchResultsTable.footerView = [ [ UIView alloc ] initWithFrame:CGRectZero ];

我使用一个表视图来显示固定数量的固定高度行,所以我简单地调整了它的大小并使其不可滚动。