【发布时间】: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表格上的视图(或视图控制器,您需要选择日期背后的逻辑)。