Reputation: 41
Iphone screenshot https://i.sstatic.net/zQ06M.png
I'm trying to make it so that my textLabel and my detailTextLabel show two different things but for some reason they both say the same thing. There should be just one cell here with the top label displaying the number 2 and the bottom label displaying the ;slakdfj(random typing for now). Refer to the links above for screenshot.
here is the main vc:
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableViewProducts: UITableView!
var delegate: ListViewController?
var ref:DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [productsList]()
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference().child("0")
loadProducts()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postData.count
}
func loadProducts() {
ref?.observe(DataEventType.value, with: { (snapshot) in
var newSweets = [productsList]()
for post in snapshot.children {
let postObject = productsList(snapshot: post as! DataSnapshot)
newSweets.append(postObject)
print(self.postData)
}
self.postData = newSweets
self.tableViewProducts.reloadData()
}) { (error:Error) in
print(error)
}
}
//This places the text on the ViewControllerTableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let sweet = postData[indexPath.row]
cell.textLabel?.text = sweet.id
cell.detailTextLabel?.text = sweet.p_name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: self)
}
}
here is the structure:
import Foundation import UIKit import FirebaseDatabase
struct productsList {
let id: String!
let p_name: String!
init(id: String, p_name: String) {
self.id = id
self.p_name = p_name
}
init (snapshot:DataSnapshot) {
id = snapshot.value as! String
p_name = snapshot.value as! String
}
}
Data Structure(is pretty basic for now):
[ {
"id" : "2",
"p_name" : ";slakdfj"
} ]
Upvotes: 0
Views: 36
Reputation: 1169
Here you set the same value into id and p_name. That's the reason.
init (snapshot:DataSnapshot) {
id = snapshot.value as! String
p_name = snapshot.value as! String
}
You need to change this code something like this:
init (snapshot:DataSnapshot) {
var dict = snapshot.value as! [String: AnyObject]
id = dict["id"] as! String
p_name = dict["p_name"] as! String
}
Here you need to change "id" and "p_name" to fit your firebase database.
Upvotes: 1