ios - 如何在 Swift 中隐藏 UITableView?

标签 ios swift

我有一个单页 iOS 应用程序(Swift 4、Xcode 9)。它有一个 UITableView 和一个 UIDatePicker。当应用程序加载时,只有表格对用户可见 - 它占据了整个屏幕。当用户单击“显示日期选择器”时,我希望表格消失,以便日期选择器出现。但我不知道该怎么做。当我设置 tableView.isHidden = true 时,表格确实消失了,但屏幕变成空白,日期选择器不可见。我做错了什么?

(我知道我可以有第二个 View Controller 、一个 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
        }
    }
}

最佳答案

这是一个简单的解决方案。将两个 View 放入一个 stackView 中。一个是隐藏的,一个不是。 将堆栈 View 约束到父 View 的框架。

添加一行代码: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

关于ios - 如何在 Swift 中隐藏 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52303532/

相关文章:

ios - swift - cocoa 类处理

swift - 如何在 swift xcode 中向小于 1.00 的答案开头添加零?

ios - 我如何从 obj-c 协议(protocol)快速实现这个方法?

php - 如何通过 PHP Web 服务器 URL 传递数组?

Android map View setShowsUserLocation?

ios - 为空 UITableView 编写条件语句

ios - 表格 View 中隐藏的自定义范围 slider

ios - 如何使 NSMutableAttributedString 响应设置应用程序中的动态类型文本

swift - 在 Swift 中检索深度嵌套的 Firebase 数据

swift - 在 Swift 3 中返回序列