![](/img/trans.png)
[英]How would I display a UIDatePicker when a UITableView cell is tapped?
[英]How to Display data from a UIDatePicker into a UITableView?
我想從UIDatePicker獲取日期並將其添加到UITableView單元格。
我嘗試將標簽放入表格視圖單元格,並在每次使用該應用的用戶點擊添加日期時進行更新。 但是,這是不合邏輯的方式,Xcode不允許我給我錯誤。
因此,我在表視圖之外放置了一個標簽以進行測試,每當我單擊“添加日期”按鈕時,就會顯示日期。
這是我的視圖控制器文件夾中的代碼
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var examDatePicker: UIDatePicker!
@IBOutlet weak var AddClassButton: UIButton!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var datesTable: UITableViewCell!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
AddClassButton.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pressButton(button: UIButton) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
let selectedDate = dateFormatter.string(from: examDatePicker.date)
dateLabel.text = selectedDate
// datesTable
//myDatePicker.datePickerMode = UIDatePickerMode.Date
}
}
您需要實現表格視圖的數據源方法,並且在從日期選擇器中選擇日期之后,您需要重新加載表格視圖並將該選定日期分配給單元格的標簽
class ViewController: UIViewController {
var selectedDate : String ? //Make global variable
func pressButton(button: UIButton) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
selectedDate = dateFormatter.string(from: examDatePicker.date)
self.IBtableView.reload()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1 //anything you want
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? YourCustomCell // Or UITableViewCell
cell. dateLabel.text = selectedDate ?? "SelectDateFromDatePicker"
}
}
這個答案與Sunil Prajapati提供的答案略有不同,表明使用數組來容納字符串,這是我認為是更標准的模式。
import UIKit
class ViewController: UIViewController {
// MARK: - Outlets
@IBOutlet weak var table: UITableView!
@IBOutlet weak var datePicker: UIDatePicker!
// MARK: - Properties
var tableSource = [String]()
// MARK: - Actions
@IBAction func pickDateButtonTouched(_ sender: Any) {
// a bit contrived, but you get the idea
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
// get the date
let dateString = formatter.string(from: datePicker.date)
// put the string in the data source for the table
tableSource.insert(dateString, at: 0)
table.reloadData()
// profit?
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = tableSource[indexPath.row]
return cell
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.