gpt4 book ai didi

ios - 如何从一个类中访问另一个类中的值? swift 4

转载 作者:行者123 更新时间:2023-11-30 11:04:47 25 4
gpt4 key购买 nike

我有一个应用程序,当用户从代表一组图像的 TableView 中点击 cell 时,他会被带到另一个 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方法中,根据indexPath.rowtableData中获取选定的数据,并将该数据传递给下一个 Controller (在下一个 Controller 并在推送/呈现之前将获取的数据分配给该变量)。

  1. didSelectItem :获取选定的数据 - let data = tableData[indexPath.row]
  2. 将其分配给下一个 Controller 的变量 -

    让 vc = nextVC()
    vc.fetchedData = 数据
    推送/呈现 vc

关于ios - 如何从一个类中访问另一个类中的值? swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52847020/

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