如何遍历我的数组
generalRoomDataArr
更改UI图像时发布到表视图的值。现在我上传了照片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
按钮/ TableView
//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!
}
火力地堡
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])
}
})
}
答案 0 :(得分:0)
您可以向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,您可以根据需要传递您的网址。但是在这种情况下,您只需要知道它是否已更新。