【发布时间】:2019-06-07 19:39:43
【问题描述】:
我在UITableView cells 中有一系列帖子。我做了一个功能,当你点击cell时,你会转到一个新的UIViewController。现在我希望新的UIViewController 根据您点击的帖子更新正文和标题。我有以下新 UIViewController 的文本:
import UIKit
import Firebase
class MainTextView: UIViewController {
@IBOutlet weak var titleText: UILabel!
@IBOutlet weak var mainText: UILabel!
func setMain(post:Post){
titleText.text = post.text
mainText.text = post.title
}
}
我的主视图控制器的以下文本:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
let row = indexPath.row
print("Row: \(row)")
let destination = storyboard!.instantiateViewController(withIdentifier: "MainTextView") as! MainTextView
navigationController?.pushViewController(destination, animated: true)
destination.setMain(post: posts[indexPath.row])
}
}
这是我的帖子类:
导入基础
类帖子{ 变量 ID:字符串 变量标题:字符串 可变文本:字符串 var createdAt:Date
init(id: String, title: String,text:String, timestamp:Double) {
self.id = id
self.title = title
self.text = text
self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)
}
}
我应该注意,我的 HomeViewController 函数 fetchPost 中也出现以下错误:
func fetchPosts(completion:@escaping (_ posts:[Post])->()) {
let postsRef = Database.database().reference().child("posts")
var queryRef:DatabaseQuery
let lastPost = posts.last
if lastPost != nil {
let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp).queryLimited(toLast: 20)
} else {
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryLimited(toLast: 20)
}
queryRef.observeSingleEvent(of: .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 title = dict["text"] as? String,
let text = dict["title"] as? String,
let timestamp = dict["timestamp"] as? Double {
if childSnapshot.key != lastPost?.id {
let post = Post(id: childSnapshot.key, title: title, text: text, timestamp:timestamp)
tempPosts.insert(post, at: 0)
}
}
}
return completion(tempPosts)
})
}
这是我的 fetchPosts 函数:
func fetchPosts(completion: @escaping( _ posts:[Post])->()) {
let postsRef = Database.database().reference().child("posts")
let lastPost = self.posts.last
var queryRef:DatabaseQuery
if lastPost == nil {
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryLimited(toLast: 10)
} else{
let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000
queryRef = postsRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
}
queryRef.observeSingleEvent(of: .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 text = dict["text"] as? String,
let comment = dict["comment"] as? Array<String>,
let title = dict["title"] as? String,
let timestamp = dict["timestamp"] as? Double{
if childSnapshot.key != lastPost?.id {
let post = Post(id: childSnapshot.key, title: text, text: title, comment: comment, timestamp: timestamp)
tempPosts.insert(post, at: 0)
}
}
}
return completion(tempPosts)
})
}
【问题讨论】:
-
destination.setMain(post: somePost). -
我用destination.setMain(post:Post)替换了setMain(post:Post)。我收到错误:无法将类型“Post.Type”的值转换为预期的参数类型“Post”。我对你在这里的意思有点困惑。我也尝试了 somePost 并出现错误...谢谢您的帮助
-
我放了
somePost,因为你需要传递Post对象的一些实例。 -
看来你是对的!我需要了解实例。这看起来是一个好的开始:stackoverflow.com/questions/45989491/…