我有一个应用程序,当用户cell
从TableView
代表一组图像的a 中点击 a 时,他会被带到另一个 tableView,其中应该显示该组中的所有图像。但是我不确定如何做到这一点。
我目前已经在开始时提取了进行引用和获取所有数据所需的信息,这些数据位于对象数组中。但是,如何从另一个类访问这些值?
数据模型:
struct UserImage {
var userID: String
var image: UIImage
var postNum: String
}
我正在创建一个数组,如下所示,在一个P1TableVC
:
let arrayOfUserImageData = [UserImage]()
检索和存储数据的函数如下所示:
func fetchAllUserFristImage() {
print("Description: calling of fetchAllUserFristImage()")
Database.database().reference().child("Posts").observe(.childAdded, with: {(snapshot) in
if snapshot.value as? [String: AnyObject] != nil {
let user = snapshot.key
print("Description: calling of snapshot.value is not nil ")
self.databaseRef = Database.database().reference()
let usersPostRef2 = self.databaseRef.child("Posts").child(user)
usersPostRef2.observe(.value, with: {(postXSnapshots) in
if let postDictionary2 = postXSnapshots.value as? [String:AnyObject] {
for (p) in postDictionary2 {
if let posts = p.value as? [String:AnyObject] {
print("Description: posts has value of: \(posts)")
//to get back to where i was delete the below for i
for (i) in posts {
if let imageUrlString = i.value as? [String:AnyObject], let postUrl = imageUrlString["image1"] as? String {
print("Description: inside the if let imageUrlString = i.value ")
self.feedArray.append(Post(fetchedImageURL: postUrl))
if let imageUrl = URL(string: postUrl), let imageDataL = try? Data(contentsOf: imageUrl), let image = UIImage(data: imageDataL) {
print("Description: inside the if let imageUrl = URL(string: postUrl)")
print("Description: img url's of posts: \(imageUrl)")
self.tableData.append(UserImage(userID: user, image: image, postNum: p.key))
self.tableView.reloadData()
} else {print("this user had no posts, was nil")}
}
}
}
}
}
})
//below shud stay same
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
})
}
在didSelectItem
方法中,从tableData
基于获取所选数据indexPath.row
并将此数据传递给下一个控制器(在下一个控制器中创建一个变量,并在推送/呈现之前将获取的数据分配给该变量)。
didSelectItem
: 获取选定的数据 - let data = tableData[indexPath.row]
将其分配给下一个控制器的变量 -
let vc = nextVC() vc.fetchedData = data push/present vc
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句