完成したコード
import Foundation
import UIKit
class ChatView :UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
var roomData:ChatRoom!
var chatData:[ChatText] = []
var database = DatabaseHelper()
var messageCount = -1
override func viewDidLoad() {
super.viewDidLoad()
database.getUserName(userID: roomData.userID, result: {
name in
self.navigationItem.title = name
})
database.chatDataListener(roomID: roomData.roomID, result: {
result in
self.chatData = result
self.messageUpdated()
})
}
func messageUpdated(){
tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return chatData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let data = chatData[indexPath.row]
let cell:UITableViewCell
if data.userID == AuthHelper().uid() {
cell = tableView.dequeueReusableCell(withIdentifier: "cell2")!
} else {
cell = tableView.dequeueReusableCell(withIdentifier: "cell1")!
}
let imageView = cell.viewWithTag(2) as! UIImageView
imageView.layer.cornerRadius = imageView.frame.height * 0.5
imageView.clipsToBounds = true
database.getImage(userID: data.userID, imageView: imageView)
let label = cell.viewWithTag(1) as! UILabel
label.text = data.text
label.layer.cornerRadius = label.frame.size.height * 0.5
label.clipsToBounds = true
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70.0
}
}
コメント