我正在创建新闻提要。我有一个视图控制器,允许用户填写信息并点击发布。部分信息涉及在分段控件中选择XBOX,PS4或PC。因此,如果他们选择XBOX,则当表格单元格中的文本标签发布到新闻提要时应显示XBOX。我在连接所有内容时遇到了麻烦,因为我是iOS编程的新手。
这是一种视图控制器,可用于发布到新闻提要:
import Foundation
import UIKit
import Firebase
import FirebaseDatabase
class NewPost: UIViewController, UITextViewDelegate {
@IBOutlet var enterGamertag: UITextField!
@IBOutlet var enterMessage: UITextField!
@IBOutlet var platformSegmentedControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//ADDTOLIST BUTTON
@IBAction func addToList(_ sender: UIButton) {
let postRef = Database.database().reference().child("posts").childByAutoId()
let postObject = [
"gamerTag": enterGamertag.text as Any,
"bodytext": enterMessage.text as Any,
"platformSelected": platformSegmentedControl.selectedSegmentIndex as Any,
"timestamp": [".sv":"timestamp"]
] as [String:Any]
postRef.setValue(postObject, withCompletionBlock: { error, ref in
if error == nil {
self.dismiss(animated: true, completion: nil)
} else {
// Handle the error
}
})
}
//dismiss keyboard
@IBAction func dismissKeyboard(_ sender: UITextField) {
self.resignFirstResponder()
}
@IBAction func micPressed(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true
}
}
@IBAction func logOutPressed(_ sender: UIButton) {
try! Auth.auth().signOut()
performSegue(withIdentifier: "logOut", sender: self)
}
@IBAction func platformSegmentPressed(_ sender: UISegmentedControl) {
let getIndex = platformSegmentedControl.selectedSegmentIndex
switch (getIndex) {
case 0:
default:
<#code#>
}
}
}
这里是新闻源视图控制器:
import UIKit
import Firebase
class FeedTable: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableFeedView: UITableView!
var posts = [Post]()
//VIEWDIDLOAD
override func viewDidLoad() {
super.viewDidLoad()
observePosts()
// Hide the navigation bar on the this view controller
tableFeedView.delegate = self
tableFeedView.dataSource = self
tableFeedView.register(UINib(nibName: "PostTableViewCell", bundle: nil), forCellReuseIdentifier: "customTableCell")
// self.tableFeedView?.backgroundColor = UIColor.black
tableFeedView.tableFooterView = UIView()
configureTableView()
}
func observePosts() {
let postRef = Database.database().reference().child("posts")
postRef.observe(.value, with: { snapshot in
var tempPosts = [Post]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let gamerTag = dict["gamerTag"] as? String,
let bodytext = dict["bodytext"] as? String,
let timestamp = dict["timestamp"] as? Double {
let post = Post(id: childSnapshot.key, gamerTag: gamerTag, bodyText: bodytext, timestamp: timestamp)
tempPosts.append(post)
}
}
self.posts = tempPosts
self.tableFeedView.reloadData()
})
}
@IBAction func refreshTable(_ sender: UIButton) {
tableFeedView.reloadData()
}
//Cell For Row At
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:PostTableViewCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell", for: indexPath) as! PostTableViewCell
cell .set(post: posts[indexPath.row])
return cell
}
//Number Of Rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
//Automatic Row Height
func configureTableView() {
tableFeedView.rowHeight = UITableViewAutomaticDimension
tableFeedView.estimatedRowHeight = 120.0
}
}
这是“发布”个人资料类:
import Foundation
class Post {
enum platform {
case XBOX1
case PS4
case PC
}
var id: String
var gamerTag: String
var bodyText: String
var timestamp: Double
var platform: platform = .XBOX1
init(id: String, gamerTag: String, bodyText: String, timestamp: Double, platform: platform) {
self.id = id
self.gamerTag = gamerTag
self.bodyText = bodyText
self.timestamp = timestamp
self.platform = platform
}
}
这是表格单元格类:
import UIKit
class PostTableViewCell: UITableViewCell {
@IBOutlet weak var customMessageBody: UILabel!
@IBOutlet weak var customConsole: UILabel!
@IBOutlet weak var ifMicUsed: UIImageView!
@IBOutlet weak var timeAdded: UILabel!
@IBOutlet weak var gameMode: UILabel!
@IBOutlet weak var customGamerTag: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func set(post:Post){
customGamerTag.text = post.gamerTag
customMessageBody.text = post.bodyText
timeAdded.text = "\(post.timestamp) minutes ago."
customConsole.text = "\(post.platform)"
}
}