SWIFT 中的 ParseUI PFQueryTableViewController

ParseUI PFQueryTableViewController in SWIFT

我正在尝试连续使用两个 PFQueryTableViewController,以便在第一个控制器中选择的行生成第二个 table 包含详细查询的视图。我认为这是常见的事情。

第二个视图控制器抛出这条消息:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "EQR-J7-VNh-view-gYk-IB-w7b" nib but didn't get a UITableView.'

我在第一个 table 中的单元格和新的视图控制器之间定义了一个 "show" segue,但是当它发生时,上面的消息出现并且第二个控制器永远不会被调用。如果我将第二个控制器 class 更改为常规 UIViewController,一切正常。

我觉得我需要将 PFQueryTableViewController 重铸为 .nib 中的 UIViewController,但我不知道该怎么做或为什么 Parse 将它定义为其他东西。谁能赐教。我来自 Swift 的观点,并且认为基于 UI.

的情节提要可以摆脱困境

代码如下:

目标(第二个)视图控制器:

import Parse
import UIKit
import ParseUI


class ClubDetailsViewController: PFQueryTableViewController {

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 = "USACourses"
    self.textKey = "course_name"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
    self.objectsPerPage = 10
}



@IBOutlet weak var detailClubName: UILabel!
@IBOutlet weak var detailAddress: UILabel!
@IBOutlet ...

第一个视图控制器:

import UIKit
import Parse
import ParseUI


class ClubTableViewController: 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 = "USAClubs"
        self.textKey = "club_name"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = true
        self.objectsPerPage = 20
    }

    override func queryForTable() -> PFQuery {
        var query = PFQuery(className: "USAClubs")
        query.orderByAscending("club_name")
        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")
        }

        // Extract values from the PFObject to display in the table cell
        if let nameClub = object?["club_name"] as? String {
            cell?.textLabel?.text = nameClub
        }
        if let clubtype = object?["club_membership"] as? String {
            cell?.detailTextLabel?.text = clubtype
        }

        return cell
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var detailScene = segue.destinationViewController as! ClubDetailsViewController

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

    override func viewDidAppear(animated: Bool) {

        // Refresh the table to ensure any data changes are displayed
        tableView.reloadData()
    } // end view did appear
}

确保您为 ClubDetailsViewController 设置的 ViewController 是 UITableViewController 而不是 UIViewController。

PFQueryTableViewController 是 UITableViewController 的子类,而 UITableViewController 又是 UIViewController 的子类。为了使您的 ViewController 符合 UITableViewController,它需要遵循某些规则,例如拥有 UITableView 而没有其他子视图。当您将 UITableViewController 拖到故事板上时,它会确保它遵循所需的规则。