[英]Why is my tableview getting an error when im using this code firebase Swift 4
嘿,我有2个tableviews一个在mainpage
于一个userprofilepage
上的一个mainpage
是表明存在,但是,在一个公共的所有帖子profilepage
是相同的,但我想改变它,以便只有该用户是当前该用户uid的帖子在线显示在profilepage
tableview中
我当前正在使用此代码,而该代码仅显示我的每条帖子,而不是仅ThatUser
上传的ThatUser
,但此代码不会给我错误提示
func loadTableViewData(){
if ((Auth.auth().currentUser?.uid) != nil) {
Database.database().reference().child("userposts").observeSingleEvent(of: .value) { (snapshot) in
if let postsDictionary = snapshot.value as? [String: AnyObject] {
for userPost in postsDictionary {
self.userPosts.add(userPost.value)
}
self.userPostsTableView.reloadData()
}
}
}
}
但是然后我将其更改为此代码,因为我将帖子也保存到用户的uid中,如数据库图像中所示,但这并没有给我错误,但是当我打开应用程序并转到userprofilepage
,应用程序崩溃了
if let uid = Auth.auth().currentUser?.uid {
Database.database().reference().child("userposts").child(uid).observeSingleEvent(of: .value) { (snapshot) in
if let postsDictionary = snapshot.value as? [String: AnyObject] {
for userPost in postsDictionary {
self.userPosts.add(userPost.value)
}
self.userPostsTableView.reloadData()
}
}
}
}
我在控制台中收到此错误
无法将类型'__NSCFString'(0x1043fcf68)的值强制转换为'NSDictionary'(0x1043fdf58)。 2017-11-23 14:17:30.616973 + 0100 Acty10 [21308:164951]无法将类型'__NSCFString'(0x1043fcf68)的值强制转换为'NSDictionary'(0x1043fdf58)。 (lldb)
我很确定这段代码应该可以正常工作,但是也许我做错了
这是我的数据库的图像
因为您可以使用userpost内的uid来查找iam,然后iam尝试以相同的方式进行检索,但是我崩溃了,请帮助我。
这是cellForRowAt
代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserProfileTableViewCell
// Configure the cell...
let userPost = self.userPosts[indexPath.row] as! [String: AnyObject]
cell.profileTitleLabel.text = userPost["title"] as? String
cell.profileContentView.text = userPost["content"] as? String
cell.profileTimeAndDateLabel.text = userPost["time"] as? String
cell.profileUsernameLabel.text = userPost["username"] as? String
cell.profileLocationAddress.text = userPost["adress"] as? String
if let imageName = userPost["image"] as? String {
let imageRef = Storage.storage().reference().child("images/\(imageName)")
imageRef.getData(maxSize: 25 * 1024 * 1024) { (data, error) -> Void in
if error == nil {
//successfull
let downloadedImage = UIImage(data: data!)
cell.profileImageView.image = downloadedImage
}else {
// error
print("there was an error downloading image: \(String(describing: error?.localizedDescription))")
}
}
}
return cell
}
谢谢你的时间 :)
试试这个避免崩溃
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserProfileTableViewCell
// Configure the cell...
if let userPost = self.userPosts[indexPath.row] as? [String: AnyObject] {
cell.profileTitleLabel.text = userPost["title"] as? String
cell.profileContentView.text = userPost["content"] as? String
cell.profileTimeAndDateLabel.text = userPost["time"] as? String
cell.profileUsernameLabel.text = userPost["username"] as? String
cell.profileLocationAddress.text = userPost["adress"] as? String
if let imageName = userPost["image"] as? String {
let imageRef = Storage.storage().reference().child("images/\(imageName)")
imageRef.getData(maxSize: 25 * 1024 * 1024) { (data, error) -> Void in
if error == nil {
//successfull
let downloadedImage = UIImage(data: data!)
cell.profileImageView.image = downloadedImage
}else {
// error
print("there was an error downloading image: \(String(describing: error?.localizedDescription))")
}
}
}
}
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.