Swift3:如果为空,则隐藏UITableView [英] Swift3 : Hide UITableView if Empty

查看:41
本文介绍了Swift3:如果为空,则隐藏UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,它用作屏幕的一部分.

I have a tableview, which I use as a part of the screen.

@IBOutlet var tableView:UITableView!

有时我有数据,有时却没有.我只想在有数据时显示表,而在没有数据时隐藏表.

Sometimes I have data, and sometimes I don't. I want to display the table only when data is present and hide it when there is no data.

我尝试添加一个小于或等于选项的高度限制.但是,如果这样做,即使有数据,tableview也会被隐藏

I tried adding a constraint of height with lesser or equal to options. But if I do that, tableview is hidden even when I have data

我该怎么办?

我在Google周围搜索,没有找到解决方案,因此请在这里询问.

I googled around and didn't find a solution, so asking here.

免责声明:我是iOS/Swift开发的新手.抱歉,如果已经回答

Disclaimer: I am new to iOS/Swift development. Sorry if this is already answered

推荐答案

您可以显示隐藏 tableView ,如下所示.

You can show hide tableView like below.

您可以使用可能用来填充数据的数组或 tableView 属性 numberOfRowsInSection 来检查计数.

You can check the count using the array with which you might be populating the data Or with the tableView property numberOfRowsInSection.

没有数据时

tableView.ishidden = true

有数据时

tableView.ishidden = false

或者您可以使用如下所示的委托方法.

Or you can use the delegate method as below.

 override func viewWillAppear(_ animated: Bool) {
        if array.isEmpty {
            self.tableView.isHidden = true
        }
        else {
            self.tableView.isHidden = false
        }
    }

更好的方法是使用didSet.

More better way would be using didSet.

var dataArray: [String] = [] {
   didSet {
      if dataArray.count > 0 {
         //Update Table Data
      } else {
         //Hide Table and show so info of no data
      }
   }
}

这篇关于Swift3:如果为空,则隐藏UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆