[英]pass data from uicollectionview in custom tablecell to viewcontroller
[英]How to pass user image from Tablecell to Viewcontroller on firebase?
我只能将用户名标签从表格单元传递给另一个Viewcontroller,而不是用户图像。 如您在下面的代码中所见,我尝试使用prepare for segue函数。 谢谢
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
class MainVc: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview2: UITableView!
var user = [User]()
override func viewDidLoad() {
super.viewDidLoad()
retrieveUsers()
}
func retrieveUsers() {
let ref = Database.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
let users = snapshot.value as! [String : AnyObject]
self.user.removeAll()
for (_, value) in users {
if let uid = value["uid"] as? String {
if uid != Auth.auth().currentUser!.uid {
let userToShow = User()
if let fullName = value["full name"] as? String,let businessType = value["Business Type"] as? String, let imagePath = value["urlToImage"] as? String {
userToShow.fullName = fullName
userToShow.businessType = businessType
userToShow.imagePath = imagePath
userToShow.userID = uid
self.user.append(userToShow)
}
}
}
}
self.tableview2.reloadData()
})
ref.removeAllObservers()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let upcoming: ViewController = segue.destination as! ViewController
let indexPath = self.tableview2.indexPathForSelectedRow!
let titleString = self.user[indexPath.row].fullName
let imageView = self.user[indexPath.row].imagePath! // i tried using this to transfer the image from tableview to viewcontroller but its not working
upcoming.imageView = imageView
upcoming.titleString = titleString
self.tableview2.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return user.count ?? 0
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview2.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UserCell
cell.userNameLbl.text = self.user[indexPath.row].fullName
cell.businessTypeLbl.text = self.user[indexPath.row].businessType
cell.userID = self.user[indexPath.row].userID
cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)
return cell
}
}
extension UIImageView {
func downloadImage(from imgURL: String!) {
let url = URLRequest(url: URL(string: imgURL)!)
let task = URLSession.shared.dataTask(with: url) {
(data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
self.image = UIImage(data: data!)
}
}
task.resume()
}
}
类ViewController:UIViewController {
@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var testLbl: UILabel!
var titleString: String!
var imageView: String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.testLbl.text = self.titleString
self.profileImage.image = UIImage(named: imageView!)
}
导入UIKit
类用户:NSObject {
var userID: String!
var fullName: String!
var imagePath: String!
var businessType: String!
}
您可以在ViewController
定义UIImage或图像名称属性,然后从MainVc
访问它。 我在下面传递了图像名称。
class ViewController: UIViewController {
@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var testLbl: UILabel!
var titleString: String!
var yourImageName:String!
override func viewDidLoad() {
super.viewDidLoad()
self.testLbl.text = self.titleString
self.imageview.image = UIImage(named:yourImageName)
}
并在准备进行传递时传递图像名称为
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let upcoming: ViewController = segue.destination as! ViewController
let indexPath = self.tableview.indexPathForSelectedRow!
let titleString = self.user[indexPath.row].fullName
let imageName = self.user[indexPath.row].imagename // access image name from user array
upcoming.titleString = titleString
upcoming.yourImageName = imageName
// here i want to pass user image as welH from cell to the viewcontroller
self.tableview.deselectRow(at: indexPath, animated: true)
}
希望能帮助到你。 快乐编码!
self.profileImage.self.downloadImage(来自:imageString)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.