【问题标题】:how to hide a UITableView in Swift?如何在 Swift 中隐藏 UITableView?
【发布时间】:2018-09-12 21:10:50
【问题描述】:

我有一个单页 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
        }
    }
}

【问题讨论】:

  • 我会将tableview 和pickerview 放在stackview 中,然后根据需要设置picker 的isHidden 属性。这会将表格视图向上移动
  • 没有关于这个 stackview 的事情(这里是总 iOS n00b)。我会调查的。
  • 一件事要考虑恕我直言? “显示日期选择器”听起来像是您希望它显示在 表格视图之上。根据用户的期望,消失的表格似乎(对我来说)是糟糕的 UI。我只想要一个日期以使用表格视图,present 表格上的视图(或视图控制器,您需要选择日期背后的逻辑)。

标签: ios swift


【解决方案1】:

这是一个简单的解决方案。将两个视图放入一个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
    }
}

【讨论】:

  • 这很酷,但我试图在日历应用程序的“新事件”屏幕中模仿“开始”和“结束”表格单元格。这两个单元格起初彼此相邻,但是当用户单击“开始”行时,选择器和另一行会滑入视图。再次单击该行时,它们会消失。你会怎么做?我想不通。然而。
  • SwiftUI 将会到来。就这样吧
猜你喜欢
  • 2016-02-13
  • 2020-07-01
  • 2014-08-19
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多