how to hide a UITableView in Swift?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



how to hide a UITableView in Swift?



I have a single page iOS app (Swift 4, Xcode 9). It has a UITableView and a UIDatePicker. When the app loads only the table is visible to the user - it takes up the whole screen. When the user clicks "show date picker" I want the table to disappear so that the date picker comes into view. But I can't figure out how to do that. The table does disappear when I set tableView.isHidden = true, but the screen just goes blank, the date picker doesn't become visible. What am I doing wrong?


tableView.isHidden = true



(I know I could have a second view controller, a segue, etc, but I'm trying to keep it simple for now.)


//
// ViewController.swift
// MyTable
//
// Created by Parzival on 9/9/18.
// Copyright © 2018 Parzival. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

// data for the table view cells
let rowNames: [String] = ["foo", "bar", "show date picker"]

// cell reuse id (cells that scroll out of view can be reused)
let cellReuseIdentifier = "cell"

// hook this up from the storyboard
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var datePicker: UIDatePicker!

override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
makeTable()


override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.


func makeTable()
// Register the table view cell class and its reuse id
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

// (optional) include this line if you want to remove the extra empty cell divider lines
self.tableView.tableFooterView = UIView()

// This view controller itself will provide the delegate methods and row data for the table view.
tableView.delegate = self
tableView.dataSource = self


// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return self.rowNames.count


// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

// create a new cell if needed or reuse an old one
let cell: UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

// set the text from the data model
cell.textLabel?.text = self.rowNames[indexPath.row]

return cell


// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
print("You tapped cell number (indexPath.row).")
if indexPath.row == 2
tableView.isHidden = true







I would put the tableview and pickerview in a stackview and then just set the isHidden property of the picker as required. This will move the tableview up
– Paulw11
7 hours ago



isHidden





Didn't about this stackview thing (total iOS n00b here). I'll look into it.
– Parzival
7 hours ago





One thing to consider IMHO? "Show date picker" sounds like you want it to be presented above the table view. Disappearing tables seems (to me) to be poor UI based on user expectations. I all you want is to get a date to use with the table view, present a view (or view controller is you need logic behind the date being picked) over the table.
– dfd
5 hours ago


present




1 Answer
1



Here is an easy solution. Put two views into a stackView. One is hidden, one is not.
constrain the stack view to the frame of parent View.



add one line code: datePicker.isHidden = false


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
print("You tapped cell number (indexPath.row).")
if indexPath.row == 2
tableView.isHidden = true
datePicker.isHidden = false




enter image description here



enter image description here





It worked right off the bat! Many thanks.
– Parzival
4 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard