Hi everyone Im practice swift by Makeschool's makestagram project, and I want to make the newest post appear at the top of a table view to let user can see the newest post first!
can anyone help? here is my code
override func viewDidLoad() {
super.viewDidLoad()
timelineComponent = TimelineComponent(target: self)
self.tabBarController?.delegate = self
}
func takePhoto(){
// instantiate photo taking class, provide callback for when photo is selected
photoTakingHelper = PhotoTakingHelper(viewController: self.tabBarController!,callback:{ (image: UIImage?)in
let post = Post()
post.image.value = image!
post.uploadPost()
}
)
}
override func viewDidAppear(animated: Bool) {
timelineComponent.loadInitialIfRequired()
}
func loadInRange(range: Range<Int>, completionBlock: ([Post]?) -> Void) {
ParseHelper.timelineRequestForCurrentUser(range) {
(result: [PFObject]?, error: NSError?) -> Void in
if let error = error {
ErrorHandling.defaultErrorHandler(error)
}
let posts = result as? [Post] ?? []
completionBlock(posts)
}
}
}
extension TimelineViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.timelineComponent.content.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell")as! PostTableViewCell
let post = timelineComponent.content[indexPath.section]
post.downloadImage()
post.fetchLikes()
cell.post = post
return cell
}
}
extension TimelineViewController: UITableViewDelegate{
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
timelineComponent.targetWillDisplayEntry(indexPath.section)
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("PostHeader")as! PostSectionHeaderView
let post = self.timelineComponent.content[section]
headerCell.post = post
return headerCell
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}
I finally figure it out!
parse's PFquery got updatedAt method
I change createdAt to updatedAt.
Now it works perfectly!