Swift 从表视图控制器隐藏背景视图

Swift 从表视图控制器隐藏背景视图,swift,Swift,我刚刚添加了一个占位符标签,当我的表视图为空时显示文本,但是我不知道如何在添加新单元格后关闭背景视图 这是添加单元格后表格视图的外观: 这是我用来显示标签的代码: override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the

我刚刚添加了一个占位符标签,当我的表视图为空时显示文本,但是我不知道如何在添加新单元格后关闭背景视图

这是添加单元格后表格视图的外观:

这是我用来显示标签的代码:

  override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.

    println(arrayObject.paymentsArray().count)
    if arrayObject.paymentsArray().count == 0 {
        backgroundLabel.text = "You haven't added any transactions yet. Tap the add button to add a new transaction."
        backgroundLabel.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
        backgroundLabel.numberOfLines = 0
        backgroundLabel.textAlignment = NSTextAlignment.Center
        backgroundLabel.sizeToFit()

        self.tableView.backgroundView = backgroundLabel
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        return 0
    } else {
        return arrayObject.paymentsArray().count
    }
}
如果arrayObject.paymentsArray().count不等于0,如何取消背景标签

编辑:

我将代码更改为:

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.

    println(arrayObject.paymentsArray().count)
    if arrayObject.paymentsArray().count == 0 {
        backgroundLabel.text = "You haven't added any transactions yet. Tap the add button to add a new transaction."
        backgroundLabel.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
        backgroundLabel.numberOfLines = 0
        backgroundLabel.textAlignment = NSTextAlignment.Center
        backgroundLabel.sizeToFit()
        backgroundLabel.hidden = false

        self.tableView.backgroundView = backgroundLabel
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        return 0
    } else {
        backgroundLabel.hidden = true
        return arrayObject.paymentsArray().count
    }
}
现在,这会按预期隐藏消息,但是表视图丢失了单元格之间的分隔线


您需要保留对背景标签的引用(例如作为viewController的属性),然后将hidden设置为true,例如

else子句中标签上的self.backgroundLabel.hidden=true

{
    self.backgroundLabel.hidden = true
    return arrayObject.paymentsArray().count
}
要收回行,您需要设置分隔符样式,将其设置为-您需要单行

{
    self.backgroundLabel.hidden = true
    return arrayObject.paymentsArray().count
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine

}

啊,当然。我应该注意到这一点。我现在遇到的另一个问题是表格视图丢失了单元格之间的分界线。我会更新我的问题,包括我的意思截图。完美!非常感谢。@user3746428我的荣幸。