2015-11-04 57 views
0

我使用Parse.com在我的應用上實現了一個Feed,基本上我正在填充一個UITableViewController並且一切正常,但是,我真的很喜歡Instagram的做法,似乎Instagram的每個單元格內都有一個UIView,就像一個header一樣,並且該視圖沿着滾動直到單元格的末尾,我試圖搜索這個內容,但是我沒有成功,經過一些研究,我意識到這個特性同樣一科,所以我決定實現我的querys節,我實現了下面的代碼:每個單元格都需要有一個Section - Parse和Swift

import UIKit 




class FeedTableViewController: PFQueryTableViewController { 

override func preferredStatusBarStyle() -> UIStatusBarStyle { 
    return UIStatusBarStyle.LightContent 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    loadCollectionViewData() 

} 


func loadCollectionViewData() { 

    // Build a parse query object 
    let query = PFQuery(className:"Feed") 

    // Check to see if there is a search term 


    // Fetch data from the parse platform 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [PFObject]?, error: NSError?) -> Void in 

     // The find succeeded now rocess the found objects into the countries array 
     if error == nil { 

      print(objects!.count) 
      // reload our data into the collection view 


     } else { 
      // Log details of the failure 

    } 
} 
} 



// 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 = "Feed" 
    self.pullToRefreshEnabled = true 
    self.paginationEnabled = false 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return objects!.count 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return "Section \(section)" 
} 

//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! FeedTableViewCell! 
    if cell == nil { 
     cell = FeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
    } 
    cell.anuncerPhoto.layer.cornerRadius = cell.anuncerPhoto.frame.size.width/2 
    cell.anuncerPhoto.clipsToBounds = true 

    // Extract values from the PFObject to display in the table cell 
    if let nameEnglish = object?["name"] as? String { 
     cell?.title?.text = nameEnglish 

    } 

    let thumbnail = object?["Photo"] as! PFFile 
    let initialThumbnail = UIImage(named: "loadingImage") 
    cell.photoImage.image = initialThumbnail 
    cell.photoImage.file = thumbnail 
    cell.photoImage.loadInBackground() 

    return cell 
} 
} 

基本上,我需要有每個小區的部分,現在,我已經成功地部分工作對於每個單元格,b問題是查詢在第一篇文章中重複。

在後端我有3個不同的帖子,所以,在應用程序的UItableview需要有3個不同內容的帖子,上面的代碼我成功地計算了帖子的數量,知道有多少節需要擁有並且我聲明我希望每個部分有一篇文章,但該應用程序顯示了具有相同第一篇文章的3個部分。

任何想法,如果我捕捉正確的Instagram功能的概念,爲什麼我在我的querys面臨這個問題?

謝謝。

回答

1

保留原始UITableViewDataSource方法和使用indexPath.section

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! FeedTableViewCell! 
    if cell == nil { 
     cell = FeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
    } 
    cell.anuncerPhoto.layer.cornerRadius = cell.anuncerPhoto.frame.size.width/2 
    cell.anuncerPhoto.clipsToBounds = true 


    let object = objects[indexPath.section] 

    // Extract values from the PFObject to display in the table cell 
    if let nameEnglish = object["name"] as? String { 
     cell?.title?.text = nameEnglish 

    } 

    let thumbnail = object["Photo"] as! PFFile 
    let initialThumbnail = UIImage(named: "loadingImage") 
    cell.photoImage.image = initialThumbnail 
    cell.photoImage.file = thumbnail 
    cell.photoImage.loadInBackground() 

    return cell 
} 
+0

曾爲獲取當前對象!我按照你的指示,這解決了我的問題,但生成另一個,也許你可以教我一遍,我打開另一個問題:[這裏viewforheaderinsection](http://stackoverflow.com/questions/33535442/dynamic-viewforheaderinsection-and-multiple -cellforrowatindexpath) –

相關問題