简体   繁体   English

为什么我在尝试为我的 UITableView 获取 UILabel 时不断收到“致命错误”?

[英]Why do I keep receiving a “fatal error” when trying to fetch a UILabel for my UITableView?

I'm a beginner coder and I've hit a seeming impasse.我是一名初学者,我似乎陷入了僵局。 I'm setting up a UITableView to display blog posts from users.我正在设置UITableView来显示用户的博客文章。 I can successfully retrieve information from my Firestore database, but am having trouble actually populating this information into my UITableViewCell .我可以成功地从我的 Firestore 数据库中检索信息,但是在将这些信息实际填充到我的UITableViewCell中时遇到了麻烦。 I think this is down to the way my 'PostTableViewCell' is set up.我认为这取决于我的“PostTableViewCell”的设置方式。 Any answers to this would be immensely appreciated.对此的任何答案将不胜感激。 Here is what I have:这是我所拥有的:

Home View Controller:主页查看 Controller:

 var postArray: [String] = []
var documents: [DocumentSnapshot] = []

let db = Firestore.firestore()
let currentUserID = Auth.auth().currentUser?.uid

    // Find the UserIDs of people following
// Where Field for those UserIDs in "Posts"


override func viewDidLoad() {
    super.viewDidLoad()
    getFollowingPosts()
    configureTableView()
    }

func getFollowingPosts() {
    let searchForFollowing = db.collection("users").document(currentUserID!).collection("Following")
    searchForFollowing.getDocuments { (snapshot, error) in
        for documents in snapshot!.documents {
            let followedUID = documents.get("uid")
            print(followedUID!)
            
            self.db.collection("posts").whereField("uid", isEqualTo: followedUID!).getDocuments { (querySnapshot, error) in
                for documents in querySnapshot!.documents {
                    let uid = documents.get("uid") as! String
                    let title = documents.get("Title") as! String
                    let ProfilePictureURL = documents.get("ProfilePictureURL") as! String
                    let username = documents.get("username") as! String
                    let content = documents.get("Content") as! String
                    self.postArray.append(title)
                    print(self.postArray)
                }
                self.tableView.reloadData()
            }
        }
    }
    }

func configureTableView() {
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(PostTableViewCell.self, forCellReuseIdentifier: "PostCell")

    // remove separators for empty cells
    tableView.tableFooterView = UIView()
    // remove separators from cells
    tableView.separatorStyle = .none
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    postArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostTableViewCell
    let post = postArray[indexPath.row]
    **cell.titleLabel.text = post** //Error is encountered here
    return cell
}

PostTableViewCell: PostTableViewCell:

class PostTableViewCell: UITableViewCell {

   
    @IBOutlet weak var usernameLabel: UILabel!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var contentLabel: UILabel!
}

As I said in the title, I keep receiving a 'Fatal Error: unexpectedly found nil while implicitly unwrapping an Optional value" whenever I try to build, leading me to believe something is wrong with my 'PostTableViewCell' - this point is marked with double asterisks in my code. Any information and answers would be massively appreciated. I'm a real beginner, and eager to progress from this standstill.正如我在标题中所说,每当我尝试构建时,我都会不断收到“致命错误:在隐式展开可选值时意外发现 nil”,这让我相信我的“PostTableViewCell”有问题——这一点被标记为双我的代码中的星号。任何信息和答案都将不胜感激。我是一个真正的初学者,并渴望从这种停滞状态中取得进步。

在此处输入图像描述

  1. Remove the following statement from your code:从您的代码中删除以下语句:
    tableView.register(PostTableViewCell.self, forCellReuseIdentifier: "PostCell")
  1. You are missing this function inside PostTableViewCell:您在 PostTableViewCell 中缺少此 function:
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在Swift中对动作进行排序时,为什么仍会收到此错误? - Why do I keep getting this error when I try to sequence my actions in Swift? 为什么在尝试使用presentViewController时出现此错误? - Why do I get this error when trying to use presentViewController? 为什么我在尝试移动鼠标时会出错? - Why do I get an error when trying to move the mouse? 尝试检查是否在Corona SDK中时为什么会出现此错误? - Why do i get this error when trying to check if in Corona sdk? 为什么我的UITableView尝试加载时出现无法出队的错误? - Why am I getting an error about being unable to dequeue when my UITableView tries to load? 从UITableView删除值时,为什么会出现EXC_BAD_ACCESS错误? - Why do I get an EXC_BAD_ACCESS Error when deleting values from a UITableView? 当我在iOS7上触摸UISearchBar时,为什么我的UISearchBar和我的UITableView Cell Header之间存在差距? - Why do I get a gap between my UISearchBar and my UITableView Cell Header when I touch the UISearchBar on iOS7? 尝试读取已发送到我的应用程序的文件时,为什么在iOS设备的OpenUrl函数中出现权限错误? - Why do I get a permission error in the OpenUrl function on an iOS device when trying to read in the file that has been sent to my app? 尝试更新UILabel时始终保持未使用表达式结果的状态 - Keep getting expression result unused when trying to update UILabel 尝试垂直对齐UILabel时为什么sizeToFit不起作用? 它只是停留在中心 - Why does sizeToFit not work when trying to vertically align my UILabel? It just stays in the center
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM