我是swift的新手,我一直在看这么多教程并查看其他人的代码,看看我如何在我的表视图控制器中创建一个可扩展的单元格。但是,我已经在同一个视图控制器中创建了一个firebase数据库。问题是,我需要将数据库中的信息放在可扩展视图单元格中,但标签除外。有谁知道我怎么做到这一点? (当你看一下下面的代码时,它会更有意义)
//
// Database.swift
// intern
//
// Created by Lani Daniels on 8/20/17.
// Copyright © 2017 Lani Daniels. All rights reserved.
//
import UIKit
import Firebase
import FirebaseDatabase
struct PostStruct {
let title: String
let message: String // How would I put this in the expandable cell?
let company: String // ...Also this in the expandable cell, but i assume it would be the same method as "let message"
}
class DatabaseViewController: UITableViewController {
var selectedCellIndexPath: NSIndexPath?
var posts: [PostStruct] = []
override func viewDidLoad() {
super.viewDidLoad()
//
let databaseRef = Database.database().reference()
databaseRef.child("Posts").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue?["title"] as? String
let message = snapshotValue?["message"] as? String
let company = snapshotValue?["company"] as? String
self.posts.insert(PostStruct(title: title ?? "", message: message ?? "", company: company ?? ""), at: 0)
self.tableView.reloadData()
})
post()
}
func post(){
let title = "Title"
let message = "Message"
let company = "Company"
let post : [String : AnyObject] = ["title" : title as AnyObject, "message" : message as AnyObject, "company" : company as AnyObject]
let databaseRef = Database.database().reference()
databaseRef.child("Posts").childByAutoId().setValue(post)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let label3 = cell.viewWithTag(3)as? UILabel; label3?.text=posts[indexPath.row].title
label3?.text=posts[indexPath.row].title
let textView2 = cell.viewWithTag(2) as? UITextView; textView2?.text=posts[indexPath.row].message
textView2?.text=posts[indexPath.row].message
let label4 = cell.viewWithTag(4)as? UILabel; label4?.text=posts[indexPath.row].company
label4?.text=posts[indexPath.row].company
return cell
}
@IBAction func appliedDataTapped(_ sender: Any) {
self.performSegue(withIdentifier: "segue", sender: self)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}