如何在Swift中使用Parse创建PFQueryTableViewController?

时间:2015-01-02 02:37:24

标签: ios swift parse-platform pfquery

我已经导入了所需的所有框架,但从那时起我就不知道该怎么做了(我是Swift的新手,而且没有任何关于Objective C的内容)。解析文档并不在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

2 个答案:

答案 0 :(得分:12)

确保您已导入所有解析SDK 创建一个 2个新的cocoa touch class',给一个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!




}

答案 1 :(得分:0)

我使用Alamofireimage框架处理图像。功能非常简单。

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