2017-03-14 34 views
0

我如何通過我的數組表視圖圖像不同步通過陣列火力地堡

generalRoomDataArr 

enter image description here

迭代改變投遞到一個表視圖,當談到UI圖像值。現在我上傳了一張照片網址圖片,但是我想在更新Firebase網址時更改數組中的圖片。我該怎麼做我該怎麼做。

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

可以回調添加到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,如果你想。但是在這種情況下,您只需要知道它是否已更新。

+0

因此,所有即時消息正在爲我的功能添加成功,這就是我所做的一切?我真的不明白? – codechicksrock

+0

UploadGeneralChatRoom(message:message) – codechicksrock

+0

如何在參數中添加成功? – codechicksrock