【问题标题】:Fail to hide empty cells in UITableView Swift无法在 UITableView Swift 中隐藏空单元格
【发布时间】:2014-08-19 16:12:33
【问题描述】:

我在 swift 应用程序中使用 UITableView,并使用我自己的自定义 UITableViewCell。

当试图隐藏 UITableView 中的空单元格时,它仍然具有白色背景。

在 UITableView 下我有一个背景图片 (UImageView)。

我已经尝试隐藏这些单元格,实际上它们是隐藏的,但我仍然有白色背景,使用以下代码:

override func viewDidLoad() {
    super.viewDidLoad()
    var tblView =  UIView(frame: CGRect(x: 0,y: 0,width: 0,height: 0))
    tblView.backgroundColor = UIColor.clearColor()
    tableView.tableFooterView = tblView

    var nipName=UINib(nibName: "CustomCell", bundle:nil)
    self.tableView.registerNib(nipName, forCellReuseIdentifier: "CustomCell")
}

【问题讨论】:

    标签: ios iphone uitableview swift


    【解决方案1】:

    Swift 3:最简单的方法

    tableView.tableFooterView = UIView()
    

    【讨论】:

      【解决方案2】:

      解决方法如下:

      tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
      tableView.tableFooterView?.isHidden = true
      tableView.backgroundColor = UIColor.clear
      

      【讨论】:

      • UIView does not have a member named 'hidden' 无论如何都不需要这条线。
      【解决方案3】:

      SWIFT 3

      tableView.tableFooterView = UIView(frame: .zero)

      【讨论】:

        【解决方案4】:

        这些都适用于 Swift 3.0

        方法 - 1

        let footerView =  UIView(frame: CGRect.zero)
        tableView.tableFooterView = footerView
        tableView.tableFooterView?.isHidden = true
        tableView.backgroundColor = UIColor.clear()
        

        方法 - 2

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

        【讨论】:

          【解决方案5】:

          针对此问题的另一个更简单的解决方案是为您的 tableView 设置一个图像的背景。如果您只需要为非数据单元格设置白色背景,则图像应该是纯白色的,或者您也可以为您的表格视图背景。因此,带有单元格的数据将具有正常背景,对于非数据单元格,它将显示您的背景图像:

           self.tableViewName.backgroundColor = UIColor (patternImage: UIImage(named:
           "imageName.jpg")!)
          

          【讨论】:

            【解决方案6】:

            更清洁的解决方案是:

            tableView.tableFooterView = UIView(frame: CGRectZero)
            

            不需要其他任何东西。

            【讨论】:

            • @user3210784 我相信这可能是公认的答案,因为它使用的代码比公认的答案少得多?
            • 绝对正确的答案。一点补充:您需要提及 tableView 的 IBOutlet 才能使其工作。
            猜你喜欢
            • 1970-01-01
            • 2014-12-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-11
            • 1970-01-01
            相关资源
            最近更新 更多