【发布时间】: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