2017-03-02 8 views
0

1.)最後に "....."を追加せずにメッセージのサイズに合わせてtableViewブロックを構成するように、チャット内でメッセージを拡張する方法を教えてください。TableViewチャット拡張メッセージ

2.)私は、データベースにメッセージをアップロードするが、コントローラを再ロードするまでtableViewには送らないでください。これを修正する最善の方法は何ですか?

3.)最後に投稿されたメッセージが表示されるようにテーブルビューを読み込みますか?私のtableViewの

画像:

1

import UIKit 
import Foundation 
import Firebase 
import FirebaseDatabase 
import FirebaseStorage 

struct postStruct { 
    let username : String! 
    let message : String! 
    let photoURL : String! 
} 

class GeneralChatroom: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var messageTextField: UITextField! 

    var generalRoomDataArr = [postStruct]() 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let ref = FIRDatabase.database().reference() 

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

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

      self.generalRoomDataArr.insert(postStruct(username: username, message: message, photoURL: firebaseUserPhotoURL), at: 0) 
      self.tableView.reloadData() 

     }) 

    } 

    @IBAction func backButtonPressed(_ sender: UIButton) { 
     self.performSegue(withIdentifier: "BackToRoom", sender: nil) 
    } 


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

     let message : String = self.messageTextField.text! 
     UploadGeneralChatRoom(message: message) //update general_room 
     //self.tableView.reloadData() 
     //UploadUserData() //Update Rank in database 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return generalRoomDataArr.count // your number of cell here 
    } 

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

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

     let usernameLabel = cell?.viewWithTag(1) as! UILabel 
     usernameLabel.text = generalRoomDataArr[indexPath.row].username 

     let messageLabel = cell?.viewWithTag(2) as! UILabel 
     messageLabel.text = generalRoomDataArr[indexPath.row].message 


     let imageView = cell?.viewWithTag(3) as! UIImageView 

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

     //User Profile image in tableview 
     if generalRoomDataArr[indexPath.row].photoURL != nil 
     { 
      //let imageView = cell?.viewWithTag(3) as! UIImageView 

      if let url = NSURL(string: generalRoomDataArr[indexPath.row].photoURL) { 

       if let data = NSData(contentsOf: url as URL) { 

        imageView.image = UIImage(data: data as Data) 
       } 
      } 
     } 







     // your cell coding 
     return cell! 
    } 

} 

答えて

1

Q.1用の応答:

あなたのメッセージラベルのラインの数を設定することができます 〜;テキストがたくさんある場合は、ラベルを垂直方向に延長します。

あなたはまた、次のようにautomaticDimensionのtableViewの行の高さを設定する必要があります -

tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 140 

Q.2用の応答:

は後にtableViewをリロードしてくださいそれをUploadUserData()に追加します。

Q.3用の応答:

あなただけではなく、一種のよりソート動作を逆にしたい場合は、

yourdataArray.reverse() 
+0

感謝にあなたを逆プロパティを使用する必要があります!あなたは最高です – nil

+0

私はnumberOfLine = 0を設定すると、それはまだ次の行に移動しません – nil

+0

固定高さの制約を割り当てるために発生することがあります。 – Aashish

関連する問題