hetelek
hetelek

Reputation: 3896

Properly displaying UIDatePicker without UITextField

I am trying to display a UIDatePicker without a UITextField. I understand that I can set a UITextField's inputView property, but I don't want to do it that way.

I have a UITableViewCell with the Right Detail style and I would like to display a UIDatePicker when the row is selected. When the UIDatePicker changes value, I will update the detail label in the cell.

The problem is, I cannot find a way to display a UIDatePicker without a UITextField. If I just create a UIDatePicker and add it to my view, it is not displayed properly because it has no background/container view (unless I set the background color), and I have to set the position manually.

Also, my table view controller's view property is a UIScrollView and if the user scrolls after adding the date picker to the view, then the date picker moves with respect to the scroll view. To counter this, I am currently adding it to the UIWindow.

Is there a better way of doing this, or should do it manually?

  1. Create the UIDatePicker
  2. Set the background color
  3. Calculate the position
  4. Add it to the window (to avoid the UIScrollView affecting it)

Thanks.

Upvotes: 0

Views: 980

Answers (1)

Syed Tariq
Syed Tariq

Reputation: 2928

Here is one way that worked for me. I am using my example below:

No need to do any complications to display the date picker. I just dragged it over the table view, control-dragged it to establish an IBOutlet (datePicker) and an IBAction (pickDate). In the viewDidLoad, I set datePicker.hidden = true (so that the picker is hidden initially). In the didSelectRowAtIndexPath I saved the indexPath into a global variable savedIndexPath then I set datePicker.hidden = false (which displays the date picker on top of the tableView). In the IBAction pickDate, get the cell that was selected and update the textLabel of the cell. And then hide the datePicker. (I tested this on the simulator). The way I implemented the IBAction, it changes the cell as soon as you lift your finger and then hides the picker. This needs to be improved.

It is there in the code below:

    import UIKit

class TableViewController: UITableViewController {

    var myDate = ""
    var savedIndexPath = NSIndexPath()

    @IBAction func pickDate(sender: UIDatePicker) {
        myDate = convertDate(sender.date)
        let currentCell = tableView.cellForRowAtIndexPath(savedIndexPath) as UITableViewCell?
        datePicker.hidden = false
        currentCell?.textLabel?.text = myDate

        datePicker.hidden = true
    }

    @IBOutlet weak var datePicker: UIDatePicker!

    override func viewDidLoad() {
        super.viewDidLoad()
        datePicker.hidden = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    let cellIdentifier = "cell"
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell!
        if cell == nil
        {
            cell = UITableViewCell( style:.Default, reuseIdentifier:cellIdentifier)
        }
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        savedIndexPath = indexPath
        datePicker.hidden = false
    }

    //MARK: - NSDate Conersion
    func convertDate(date:NSDate) -> String{
        let dateFormatter = NSDateFormatter()

        var theDateFormat = NSDateFormatterStyle.ShortStyle
        let theTimeFormat = NSDateFormatterStyle.ShortStyle

        dateFormatter.dateStyle = theDateFormat
        dateFormatter.timeStyle = theTimeFormat
        let dateTimeString = dateFormatter.stringFromDate(date)
        return dateTimeString
    }


}

Upvotes: 0

Related Questions