0
votes

The following is successful at pulling the "category" into the main Title until I start trying get createdAt into the subtitle.

When I start to override func tableView to import the createdAt into the subtitle, the app crashes.

Any thoughts?

import UIKit; import Parse

class UserRecordsTableViewController: PFQueryTableViewController {

    // Initialise the PFQueryTable tableview
    override init!(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Configure the PFQueryTableView
        self.parseClassName = "Event"
        self.textKey = "category"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false

    }

    // Define the query that will provide the data for the table view
    override func queryForTable() -> PFQuery! {
        var query = PFQuery(className: "event")
        query.orderByAscending("createdAt")
        query.whereKey("user", equalTo: PFUser.currentUser())



        return query
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {


        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as PFTableViewCell!

        if cell == nil {
            cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }


        //Date for cell subtitle
        var dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let dateForText = object["createdAt"] as? NSDate
        cell.detailTextLabel?.text =        dateFormatter.stringFromDate(dateForText!)

        return cell
    }

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using [segue destinationViewController].

    }

}

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

2

2 Answers

0
votes

detailTextLabel will be nil in a cell of the default type. If you made the cell in the storyboard, set its type to "Subtitle". If you didn't make it in the storyboard, then change the line where you create the cell to,

cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
0
votes

Heres what I ended up going with and this works so far. The problem was that PFTableViewCell is only intended to work with default not subtitle. So I had to create a custom cell. Next finally found the solution to the date fix on another post.

import UIKit; import Parse

class UserRecordsTableViewController: PFQueryTableViewController {

    // Initialise the PFQueryTable tableview
    override init!(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Configure the PFQueryTableView
        self.parseClassName = "Event"
        self.textKey = "category"
        self.title = "createdAt"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false

    }

    // Define the query that will provide the data for the table view
    override func queryForTable() -> PFQuery! {
        var query = PFQuery(className: "event")
        query.orderByAscending("createdAt")
        query.whereKey("user", equalTo: PFUser.currentUser())







        return query
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {


        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UserRecordsTableViewCell!

        if cell == nil {
            cell = UserRecordsTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }

    var dateUpdated = object.createdAt as NSDate
    var dateFormat = NSDateFormatter()
    dateFormat.dateFormat = "EEE, MMM d, h:mm a"

    cell.catDate.text = NSString(format: "%@", dateFormat.stringFromDate(dateUpdated))
    cell.catTitle.text = object["category"] as String!





        return cell
    }

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using [segue destinationViewController].
        /*var detailScene = segue.destinationViewController as YourDetailViewController*/

        // Pass the selected object to the destination view controller.
        /*if let indexPath = self.tableView.indexPathForSelectedRow() {
            let row = Int(indexPath.row)
            detailScene.currentObject = objects[row] as? PFObject
        }*/
    }
}