Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Michael McKenna
Michael McKenna
3,451 Points

How can I retrieve the date from a date picker inside a custom cell? (Swift)

I'm using expandable/collapsible views using the stack view, and when the first row is tapped, two text fields and a date picker appear. Everything else so far has worked except the date picker. It is always only returns today's date, and it doesn't respond to the date changing. Anyway here's the code:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:      NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CreateEvent1
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:Selector("datePickerAction:"))
    cell.datePicker.addGestureRecognizer(tapGestureRecognizer)
    return cell
    }

      func datePickerAction(sender: AnyObject) {
        var dateFormatter = NSDateFormatter()
        self.date = sender.date
     }

So regardless, self.date always returns today's date and doesn't change.

Jhoan Arango
Jhoan Arango
14,575 Points

Please help me understand what you are trying to do a bit more..

Flow of the app, what your intention is, etc..

This way i will be able to help you

Michael McKenna
Michael McKenna
3,451 Points

My intention is to get the date from the date picker which is located inside the custom cell. Sorry Jhoan I have no idea what other information I need to give you. I posted the relevant code.

Jhoan Arango
Jhoan Arango
14,575 Points

Perhaps you may want to follow this tutorial... Seems like your code may be missing some more set up on the datepicker method.

Michael McKenna
Michael McKenna
3,451 Points

That was correct. I was improperly using the tapGestureRecognizer when I should've used the "addTarget" method. Thank you