2015-11-04 58 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) –

相关问题