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

如何移除这些单元格?


当前回答

在Swift中移除UITableView中空行的额外分隔行

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.yourTableview.tableFooterView = UIView()
}

其他回答

快捷便捷的Swift 4方式。

override func viewDidLoad() {
     tableView.tableFooterView = UIView(frame: .zero)
}

如果你有静态单元格。您也可以从检查器窗口关闭分隔符。(如果你需要分隔符,这是不可取的。在这种情况下,使用上述方法)

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

iOS 7+使用故事板

简单地拖一个UIView到你的UITableView作为页脚。设置页脚视图的高度为0。

Swift 4.0扩展

只是一个小扩展的故事板:

extension UITableView {
    @IBInspectable
    var hideSeparatorForEmptyCells: Bool {
        set {
            tableFooterView = newValue ? UIView() : nil
        }
        get {
            return tableFooterView == nil
        }
    }
}

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

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

- (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]。如果您在表格底部没有看到一些红色条,则表示您的页脚没有设置。