[英]how to hide a UITableView in Swift?
我有一個單頁iOS應用程序(Swift 4,Xcode 9)。 它具有一個UITableView和一個UIDatePicker。 當應用加載時,用戶只能看到該表-它占據了整個屏幕。 當用戶單擊“顯示日期選擇器”時,我希望表格消失,以便日期選擇器進入視圖。 但是我不知道該怎么做。 當我設置tableView.isHidden = true
,表格確實消失了,但是屏幕只是一片空白,日期選擇器不可見。 我究竟做錯了什么?
(我知道我可以有第二個視圖控制器,segue等,但是我現在嘗試使其保持簡單。)
//
// 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
}
}
}
這是一個簡單的解決方案。 將兩個視圖放入stackView中。 一種是隱藏的,一種不是。 將堆棧視圖約束到父視圖的框架。
添加一行代碼: 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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.