Thứ Tư, 8 tháng 5, 2019

SpickerView Swift



import UIKit

class TimePopupVC: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    @IBOutlet weak var viewContent: UIView!
    @IBOutlet weak var pickerTime: UIPickerView!
    
    var delegate: AddCalendarVCProtocol?
    var listTime: [TimeSlotDTO] = []
    var listTimeValue: [String] = []
    var selectedIndex: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        for item in listTime {
            listTimeValue.append(item.getValue())
        }
        self.pickerTime.delegate = self
        self.pickerTime.dataSource = self
    }
    
    @IBAction func btnOKTapped(_ sender: Any) {
        delegate?.setTime(selectedIndex)
        self.dismiss(animated: true, completion: nil)
    }
    
    
    // Number of columns of data
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    // The number of rows of data
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return listTimeValue.count
    }
    
    // The data to return fopr the row and component (column) that's being passed in
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return listTimeValue[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedIndex = row
    }
}


Không có nhận xét nào:

Đăng nhận xét