【发布时间】:2017-11-20 21:31:07
【问题描述】:
我创建了一个小应用程序,用户可以将post 上传到 firebase 数据库,然后在表格视图中,所有用户的内容都显示得很像 Instagram。我遇到了一个小问题:用户的posts 是按字母顺序显示的,而不是按最新上传的posts 显示在tableview 开头的顺序。
这是我从 firebase 下载数据并在我的tableView 中查看的代码。有谁知道出了什么问题,或者为什么帖子按字母顺序显示?
import UIKit
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth
import FirebaseCore
import Firebase
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var postsTableView: UITableView!
var posts = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
loadData()
self.postsTableView.delegate = self
self.postsTableView.dataSource = self
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadData() {
Database.database().reference().child("posts").observeSingleEvent(of: .value) { (snapshot) in
if let postsDictionary = snapshot.value as? [String: AnyObject] {
for post in postsDictionary {
self.posts.add(post.value)
}
self.postsTableView.reloadData()
}
}
}
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PostTableViewCell
// Configure the cell...
let post = self.posts[indexPath.row] as! [String: AnyObject]
cell.titleLabel.text = post["title"] as? String
cell.contentTextView.text = post["content"] as? String
if let imageName = post["image"] as? String {
let imageRef = Storage.storage().reference().child("images/\(imageName)")
imageRef.getData(maxSize: 25 * 1024 * 1024) { (data, error) -> Void in
if error == nil {
//successfull
let downloadedImage = UIImage(data: data!)
cell.postsImageView.image = downloadedImage
}else {
// error
print("there was an error downloading image: \(String(describing: error?.localizedDescription))")
}
}
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400.0
}
@IBAction func logOutButton(_ sender: Any) {
do {
try Auth.auth().signOut()
let registerSuccess = self.storyboard?.instantiateViewController(withIdentifier: "SignInVC")
self.present(registerSuccess!, animated: true, completion: nil)
}catch {
let logInAlert = UIAlertController(title: "Ops something went wrong", message: "try again", preferredStyle: .alert)
logInAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(logInAlert, animated: true, completion: nil)
}
let registerSuccess = self.storyboard?.instantiateViewController(withIdentifier: "SignInVC")
self.present(registerSuccess!, animated: true, completion: nil)
}
}
这是我在数据库中的结构的图像
如果有人能解释为什么它们按字母顺序显示,那就太好了。
【问题讨论】:
-
这很可能与您的查询有关,但请发布您的数据库结构以便我确认。我们需要知道您是如何存储帖子的
-
@DoesData 我现在已经编辑了这个问题,我也会从我的数据库在 firebase 中的样子得到一张图片
-
我需要查看数据库结构。您只需要上传上传代码。
-
@DoesData 抱歉花了一些时间,但现在我得到了照片,我已经解释了我上传它们的顺序以及它们在应用程序中的显示方式
标签: ios uitableview firebase firebase-realtime-database swift4