0

I'm trying to hide table view and remove space. But the table view space not removed. When first time user coming this page there is no data inside the table view so I write ishidden=true.table hidden but space not remove.user add data manually and table view appear.this working proper after add data . I'm trying

self.tableView.tableFooterView = UIView()
self.tableView.ishidden=true

above code table hidden but not remove table space. So how to remove space of hide table view..

Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
Faster
  • 41
  • 2
  • 11
  • You need to update frame of tableview in this case. – OnkarK Sep 24 '18 at 05:53
  • You need to add `UITableView` height constraint and when there's no data change `UITableView` height constraint to 0 and when there's data again change `UITableView` height constraint. – Kuldeep Sep 24 '18 at 05:55
  • What does “*space*” means? Did you mean the empty cells? – Mannopson Sep 24 '18 at 05:58
  • post the whole code bro where you have written this code – Devil Decoder Sep 24 '18 at 06:19
  • Are you thinking of 'hide' in iOS as Visibility.Gone in android, which also wraps the content? If so, thats not how iOS views work. The 'space' taken by views and their visibility are completely unrelated properties in iOS. – Swapnil Luktuke Sep 24 '18 at 09:11

3 Answers3

0

you can do with the help of tableview contentSize observer key.For that you have to take Outlet of tableview height and set into tableView observer method. Set tableView scroll disable.

 override func viewWillAppear(_ animated: Bool) {

        tbl.addObserver(self, forKeyPath: "contentSize", options: [.new], context: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        tbl.removeObserver(self, forKeyPath: "contentSize")
    }


    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if object is UITableView {
            print("contentSize:= \(tbl.contentSize.height)")
            self.heightTbl.constant = tbl.contentSize.height
        }
    }

Hope this will help you.

Yash Bhikadiya
  • 188
  • 1
  • 9
0

You need to set the height constraint for table view. The best place to set the height is numberOfSectionsInTableView function. This function called once on every reload of data.

//Assuming your data source
var dataSource: [String] = []

//connect this outlet to tableViewHeight Constraint
@IBOutlet var tableViewHeight: NSLayoutConstraint!

//connect this outlet to tableView
@IBOutlet var tableView: UITableView!

func numberOfSections(in tableView: UITableView) -> Int {

    //need to check the data availability
    if self.dataSource.count == 0{
        //need to set the table height constraint to zero
        self.tableViewHeight.constant = 0
        self.tableView.isHidden = true
        return 0
    }
    else {
        //need to set the table height constraint to desired frame.
        self.tableViewHeight.constant = self.view.frame.size.height //Assuming full screen.
        self.tableView.isHidden = false
        return 1

    }
Naresh
  • 869
  • 8
  • 17
0

If you just want to hide tableView then just set the tableView.alpha to 0 and disable tableView interaction. You can set your no data view before tableview in storyboard hierarchy and then manage with tableview alpha values to hide and show no data view

Ashish
  • 204
  • 1
  • 8