【问题标题】:makestagram - swift list post in table view ordered from newest to oldmakestagram - 表格视图中的快速列表帖子从最新到旧排序
【发布时间】:2016-08-26 12:04:20
【问题描述】:

大家好,我通过 Makeschool 的 makestagram 项目练习 swift, 我想让最新的帖子出现在表格视图的顶部 让用户能第一时间看到最新的帖子!

谁能帮忙? 这是我的代码

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
}

}

【问题讨论】:

  • 这是Does Not Work 的帖子。您没有充分描述您的问题,因此,如果您没有 edit your question 描述您的问题,您的问题可能会被搁置。我建议使用MCVE。
  • 在所有帖子的顶部,从最新到旧列出!
  • 如果 self.posts 是表的数据源,那么它应该可以工作,因为你将它添加到数组的前面,因为它不起作用,那么还有其他事情发生,所以你需要显示更多相关代码。
  • 感谢您的建议我已经改变了描述我的问题的方式!英语不是我的母语,所以我的语法可能不正确,误会请见谅!
  • 您需要显示更多代码,特别是 cellForRowAtIndexPath 和 numberOfRowsInSection

标签: ios swift social-media


【解决方案1】:

我终于明白了!

parse 的 PFquery 得到了 updateAt 方法

我将 createdAt 更改为 updatedAt。

现在完美运行了!

【讨论】:

  • 干得好。在旁注中,现在您是否发现您的帖子有任何问题?您从未提到过您正在使用 parse,也没有包含任何查询 parse 后端以获取数据的代码。将来,为了调试帮助,请始终花时间思考可能存在问题的区域,并清楚地向其他人简要介绍您的问题。
  • 明白了!谢谢你的建议,我从中学到了很多!下次我会做得更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
相关资源
最近更新 更多