将日期拉入tableview控制器PFTableView的副标题

时间:2015-03-30 01:09:56

标签: ios swift parse-platform

以下是成功拉动"类别"进入主标题,直到我开始尝试get created进入副标题。

当我开始覆盖func tableView以将createdAt导入字幕时,应用程序崩溃了。

有什么想法吗?

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].

    }

}

致命错误:在展开Optional值时意外发现nil (lldb)

2 个答案:

答案 0 :(得分:0)

detailTextLabel将在默认类型的单元格中为nil。如果您在故事板中创建了单元格,请将其类型设置为“Subtitle”。如果您未在故事板中创建,请将创建单元格的行更改为

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

答案 1 :(得分:0)

继承了我最终的目标并且到目前为止这是有效的。问题是PFTableViewCell仅用于默认而不是字幕。所以我不得不创建一个自定义单元格。接下来终于在另一篇文章中找到了日期修复的解决方案。

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
        }*/
    }
}