gpt4 book ai didi

ios - TableView 的单元格未显示

转载 作者:行者123 更新时间:2023-11-28 06:06:52 24 4
gpt4 key购买 nike

我正在尝试编写一个社交媒体应用程序,我希望帖子显示在单元格中。单元格中唯一的东西是帖子的标签和主题的标签。当我运行代码时没有错误,但表格中没有显示单元格。我似乎无法弄清楚问题所在。谢谢你们的帮助。

import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
import FirebaseAuth
import SwiftKeychainWrapper

class FeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var postBtn: UIButton!
@IBOutlet var tableView: UITableView!

var posts = [Post]()
var post: Post!
var userName: String!

var refPosts: DatabaseReference!


override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self
//getting a reference to the node posts
refPosts = Database.database().reference().child("posts")

//observing the data changes
refPosts.observe(DataEventType.value, with: { (snapshot) in

//if the reference have some values
if snapshot.childrenCount > 0 {

//clearing the list
self.posts.removeAll()

//iterating through all the values
for data in snapshot.children.allObjects as! [DataSnapshot] {
//getting values
let postObject = data.value as? [String: AnyObject]
let id = postObject?["id"]
let thought = postObject?["thought"]
let likes = postObject?["likes"]

//creating artist object with model and fetched values
let post = Post(_id: id as! String, _thought: thought as! String?, _likes: likes as! Int?)
//appending it to list
self.posts.append(post)
}

//reloading the tableview
self.tableView.reloadData()
}
})


/*refPosts.observe(.value, with: {(snapshot) in
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] { //all objects in snapshot

self.posts.removeAll()

for data in snapshot {
print(data)
if let postDict = data.value as? Dictionary<String, AnyObject>{
let key = data.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
}
}
}
self.tableView.reloadData()
})*/

}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell{
cell.configCell(post: post)
return cell
}else{
return PostCell()
}
}

func PostToFirebase(imgUrl: String){
let userID = Auth.auth().currentUser?.uid

Database.database().reference().child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
if let data = snapshot.value as? Dictionary<String, AnyObject>{
//if let snap = snapshot.value as? [String:Any] {
let data = snapshot.value as! Dictionary<String, AnyObject>

let username = data["username"]

let post: Dictionary<String, AnyObject> = [
"username": username as AnyObject,
"likes": 0 as AnyObject
]
let firebasePost = Database.database().reference().child("posts").childByAutoId()
firebasePost.setValue(post)
self.tableView.reloadData()
}
})
}

@IBAction func postImageTapped(_ sender: AnyObject){//leads to text field box -> What are you thinking?
//present(imagePicker, animated: true, completion: nil)
//performSegue(withIdentifier: "UploadPage", sender: nil)
}

@IBAction func SignOut (_ sender: AnyObject){

try! Auth.auth().signOut()

KeychainWrapper.standard.removeObject(forKey: "uid")

dismiss(animated: true, completion: nil)
}
}

最佳答案

正如@vadian 在评论中提到的那样,考虑到您已正确设置所有内容,在这种情况下强制转换会很好。尝试添加我在下面的打印语句,这样您就可以更详细地了解单元格发生了什么。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell {
print("dequeueing cell")
cell.configCell(post: post)
return cell
}else{
print("initialising cell")
return PostCell()
}
}

解决方案涉及使用采用索引路径并强制转换的较新的出列方法。

let post = posts[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostCell

cell.configCell(post: post)

return cell

关于ios - TableView 的单元格未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47931058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com