gpt4 book ai didi

ios - 通过数组 Firebase 实现 TableView 图像异步

转载 作者:行者123 更新时间:2023-11-30 12:36:40 27 4
gpt4 key购买 nike

如何迭代我的数组

generalRoomDataArr

enter image description here

在涉及 UI 图像时更改发布到 TableView 的值。现在我上传了一张照片 URL 图像,但我想在更新 firebase URL 时更改数组中的图像。我能做什么我应该怎么做。

func initializeArray() {

let ref = FIRDatabase.database().reference()
//let userID = FIRAuth.auth()?.currentUser?.uid

ref.child("general_room").queryOrderedByKey().observe(.childAdded, with: {snapshot in

let snapDict = snapshot.value as? NSDictionary
let message = snapDict?["Message"] as? String ?? ""
let username = snapDict?["user_name"] as? String ?? ""
let userIDString = snapDict?["uid"] as? String ?? ""
let firebaseUserPhotoURL = snapDict?["photo_url"] as? String ?? ""

//Time String from Firbase Database
let timeString = snapDict?["time_stamp"] as? String ?? "2017-03-06 00:20:51"

//timeAgoSinceDate - Format and call function to recieve time of post
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let timeStampDate = dateFormatter.date(from: timeString)
let timeStamp = timeAgoSinceDate(date: timeStampDate!, numericDates: false)


//Assign array values
self.generalRoomDataArr.insert(postStruct(username: username, message: message, photoURL: firebaseUserPhotoURL, timeStamp: timeStamp, cellUserId: userIDString), at: 0)
self.tableView.reloadData()


})

}//END FUNCTION

按钮/表格 View

//Message Send button is pressed data uploaded to firebase
@IBAction func sendButtonPressed(_ sender: UIButton) {


//If a character exists will be uploaded to firebase
if ((messageTextField.text?.characters.count)! > 0) {

let message : String = self.messageTextField.text!


UploadGeneralChatRoom(message: message) //upload to general_room


self.messageTextField.text = nil
messageTextField.resignFirstResponder()//Quit keyboard

self.tableView.reloadData() //Reload tableView
//UploadUserData() //Update Rank in database

}

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell")


//initialize UI Profile Image
let imageView = cell?.viewWithTag(3) as! UIImageView

//Make Porfile Image Cirlce
imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true


//Loading and change of Usesrs profile image on chat cell
let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL

//Load profile image(on cell) with URL & Alamofire Library
let downloadURL = NSURL(string: userProfileChatImage!)
imageView.af_setImage(withURL: downloadURL as! URL)



// your cell coding
return cell!
}

Firebase

func UploadGeneralChatRoom(message : String) {

//Firebase Initialization
var ref: FIRDatabaseReference!
//var storage: FIRStorageReference!
let userID = FIRAuth.auth()?.currentUser?.uid
ref = FIRDatabase.database().reference()
//storage = FIRStorage.storage().reference()


//Get Data from database resend to database
if let userId = userID {
ref.child("Users").child(userID!).observeSingleEvent(of: .value, with: {(snapshot) in

let snapDict = snapshot.value as? NSDictionary
let username = snapDict?["Username"] as? String ?? ""
let firebaseUserPhotoURL = snapDict?["photo_url"] as? String ?? ""
let userRank = snapDict?["user_rank"] as? String ?? ""

let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nowString = dateFormatter.string(from: now)


//Update general_room information about user
ref.child("general_room").childByAutoId().setValue(["user_name": username, "uid": userId, "Message" : message, "time_stamp" : nowString, "photo_url" : firebaseUserPhotoURL])


//Update User Rank - transforming String to Int back to String for firebase
if var userRankInt = Int(userRank) {
userRankInt += 1 //Increment User Rank every post
let userRankString = String(userRankInt) //Convert Int back to String
//Update User Rank of the current user everytime they post
ref.child("Users").child(userID!).updateChildValues(["user_rank": userRankString])

}



})

}

最佳答案

您可以向 UploadGeneralChatRoom 方法添加回调。例如:

func UploadGeneralChatRoom(message : String, success: (()->())?)

然后在收到快照时调用成功回调;

ref.child("Users").child(userID!).observeSingleEvent(of: .value, with: {(snapshot) in
success?()
let snapDict = snapshot.value as? NSDictionary
let username = snapDict?["Username"] as? String ?? ""
let firebaseUserPhotoURL = snapDict?["photo_url"] as? String ?? ""
let userRank = snapDict?["user_rank"] as? String ?? ""

let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nowString = dateFormatter.string(from: now)


//Update general_room information about user
ref.child("general_room").childByAutoId().setValue(["user_name": username, "uid": userId, "Message" : message, "time_stamp" : nowString, "photo_url" : firebaseUserPhotoURL])


//Update User Rank - transforming String to Int back to String for firebase
if var userRankInt = Int(userRank) {
userRankInt += 1 //Increment User Rank every post
let userRankString = String(userRankInt) //Convert Int back to String
//Update User Rank of the current user everytime they post
ref.child("Users").child(userID!).updateChildValues(["user_rank": userRankString])

}



})

此成功回调返回void,您可以根据需要传递您的url。但在这种情况下,您只需要知道它是否更新即可。

关于ios - 通过数组 Firebase 实现 TableView 图像异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42780131/

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