【问题标题】:How to create a PFQueryTableViewController with Parse in Swift?如何在 Swift 中使用 Parse 创建一个 PFQueryTableViewController?
【发布时间】:2025-12-01 19:15:01
【问题描述】:

我已经导入了所有需要的框架,但从那时起我就不太知道该怎么做了(我是 Swift 的新手,对 Objective C 没有绝对的了解)。 Parse docs 还没有在 Swift 中,所以有人可以为我提供一个起点吗?

我已经初始化了我的视图控制器(当我运行应用程序时它没有出现,我只是得到一个黑屏;但根据这个视频,我应该看到一个表格:https://parse.com/tutorials/parse-query-table

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //Link to Parse
    Parse.setApplicationId("...", clientKey: "...")

    var controller:PFQueryTableViewController = PFQueryTableViewController(className: "test1")
    self.window?.rootViewController = controller
    self.window?.makeKeyAndVisible()

    return true

【问题讨论】:

  • 我知道这不能回答你的问题,但 Obj C 和 swift 是可互操作的。您暂时可以下载 Obj C 的 Parses 模板项目,因为该语言的教程要多得多。即使您能够启动并运行您的应用程序,您将来也可能会遇到错误,因为缺乏教程来指导您快速完成它。您可以随时转换您的obj c project to swift。由于您对这两种语言都是新手,因此您必须从头开始

标签: ios swift parse-platform pfquery


【解决方案1】:

确保您已导入所有解析 SDK 创建一个 2个新的可可触摸类',给一个PFQueryTableViewController的子类和另一个PFTableViewCell。将它们连接到故事板中。确保 Tableview 和单元格指向这些文件。您的 Tableview 文件应如下所示;

import UIKit

class YourTableViewController: 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 = "yourClass"

    self.textKey = "yourObject"
    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: "yourClass")
    query.orderByAscending("yourObject")


    return query
}

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomTableViewCell!
    if cell == nil {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }

    // Extract values from the PFObject to display in the table cell

    cell.info.text = object["info"] as String


    // Date for cell subtitle
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let dateForText = object["date"] as NSDate
    cell.date.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].
    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
    }
}


Make sure you have set the cells reuse identifier to match what you are setting it to in this code. 

当你打电话给 cell.info 时,cell.date。这些是我在 CustomTableViewCell 文件中设置的 IBOutlets。

class CustomTableViewCell: PFTableViewCell {

@IBOutlet weak var info: UILabel!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var mixCoverPhotoImageView: PFImageView!




}

【讨论】:

  • 确保在您的 CustomTableViewCell 中您还导入了必要的 Parse 库,如下所示: import UIKit import Parse import ParseUI
  • 如何处理表格中的图像?我的意思是你如何设置 cell.mixcoverphotoimageview 属性覆盖 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell
【解决方案2】:

我已经使用 Alamofireimage 框架处理图像。功能真的很简单。

Alamofire.request(myURL).responseImage(completionHandler: { (response) in
            if let ThumbImage = response.result.value {
                DispatchQueue.main.async{
                cell?. mixCoverPhotoImageView?.image = ThumbImage
            }

【讨论】:

  • 这根本没有回答原来的问题。
最近更新 更多