ios - TableView 聊天扩展消息

标签 ios swift firebase firebase-realtime-database tableview

1.) 我如何扩展聊天中的消息,以便将 tableView block 配置为消息的大小,而不在末尾添加“.....”。

2.) 当我点击发送时,消息会上传到数据库,但不会上传到 tableView,直到我重新加载 Controller ?解决此问题的最佳方法是什么?

3.) 加载表格 View 以便在底部显示最后发布的消息?

我的 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!
    }

}

最佳答案

Answer for Q.1:

您可以将消息标签行数设置为0;如果您有很多文本,它会垂直扩展您的标签。

您可能还需要将 tableView 的行高设置为 automaticDimension,如下所示:-

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140

Answer for Q.2:

在将 tableView 添加到 UploadUserData() 后尝试重新加载它。

Answer for Q.3:

如果你只想反转排序行为而不是排序,你需要使用 reverse 属性

yourdataArray.reverse()

关于ios - TableView 聊天扩展消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42547172/

相关文章:

objective-c - 手势识别器的正确 MVC 模式

ios - BFTask 的值类型没有成员生成器 : Swift 2. 0

ios - Parse PushError 141 invalid function 云代码

java - Firebase 到 ImageView - 为什么即使没有图像,我的聊天也会显示图像?

ios - 可以将两个 iOS 应用程序与同一个 Firestore 数据库一起使用吗?

firebase - 云功能增量计数器,不读取文档

ios - 两个按钮与自动布局相冲突

iphone - AVAudioPlayer 后台任务不起作用 [iOS]

iOS-管理和跟踪多个并发任务

ios - 使用 UIImageView 或 UIView 快速绘图