![](/img/trans.png)
[英]Use PFQueryTableViewController (from parse) with Swift
[英]How to create a PFQueryTableViewController with Parse in Swift?
我已經導入了所有需要的框架,但從那時起我就不知道該怎么做了(我是Swift的新手,而且沒有任何關於Objective C的內容)。 解析文檔不在Swift中,那么有人可以給我一個起點嗎?
我已經初始化了我的視圖控制器(當我運行應用程序時沒有出現,我只是得到一個黑屏;但基於這個視頻,我應該看到一個表: https : //parse.com/tutorials/parse-查詢表 )
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
確保已導入所有解析SDK的Create a 2 new 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!
}
我用Alamofireimage框架處理圖像。 功能非常簡單。
Alamofire.request(myURL).responseImage(completionHandler: { (response) in
if let ThumbImage = response.result.value {
DispatchQueue.main.async{
cell?. mixCoverPhotoImageView?.image = ThumbImage
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.