简体   繁体   中英

Updating date(time) in cell after selecting DatePicker

I'm currently an app that has a feature that allows users to add schedule(time)

Here's the progress I made so far
1. Added textview and datepicker and made the datepicker enable after tapping the textView
2. Time (the time I run the project) is displayed on each cell(as attached picture)

I want to display date(like 10:10PM in the screen short) in the cell after selecting time in datepicker, but unable to update the selected time.
Any ideas how to do this?
Thanks in advance.

TimeSelectorViewController:

import UIKit

class TimeSelectorController: UICollectionViewController, 
UICollectionViewDelegateFlowLayout, UITextViewDelegate {

let cellId = "cellId"

let timeSelectorCell = TimeSelectorCell()


override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.backgroundColor = .yellow

    collectionView.reloadData()
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "V", style: .plain, target: self, action: #selector(handleBack))

    collectionView.register(TimeSelectorCell.self, forCellWithReuseIdentifier: cellId)
}


@objc fileprivate func handleBack() {
    dismiss(animated: true, completion: nil)
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! TimeSelectorCell

    cell.textView.inputView = timeSelectorCell.timePicker
    cell.textView.text = timeSelectorCell.handleSelectedTime()

    return cell
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: view.frame.width, height: 50)
   }

}

TableSelectorCell:

import UIKit

class TimeSelectorCell: UICollectionViewCell {


let textView: UITextView = {
    let tv = UITextView()
    tv.text = "Time Goes Here"
    tv.isEditable = false
    return tv
}()

lazy var timePicker: UIDatePicker = {
    let picker = UIDatePicker()
    picker.datePickerMode = .time
    picker.minuteInterval = 10
    //picker.minuteInterval = 30
    picker.addTarget(self, action: #selector(handleSelectedTime), for: .valueChanged)
    return picker
}()

@objc func handleSelectedTime() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    let selectTime = dateFormatter.string(from: timePicker.date)

    textView.inputView = timePicker
    textView.text = selectTime

    print(selectTime)

    return selectTime
}


override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(textView)
    textView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, height: 0, width: 0)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
   }
 }

将遵循您的dateFormat以获取12小时格式的时间

dateFormatter.dateFormat = "h:mm a"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM