채팅방에서 상대방 이름과 마지막 메세지 출력되도록 추가
//
// ChatRoomsViewController.swift
// FreeTalk
//
// Created by stayfoolish on 2018. 9. 16..
// Copyright © 2018년 stayfoolish. All rights reserved.
//
import UIKit
import Firebase
class ChatRoomsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var uid : String!
var chatrooms : [ChatModel]! = []
override func viewDidLoad() {
super.viewDidLoad()
self.uid = Auth.auth().currentUser?.uid
self.getChatroomsList()
// Do any additional setup after loading the view.
}
func getChatroomsList(){
Database.database().reference().child("chatrooms").queryOrdered(byChild: "users/"+uid).queryEqual(toValue: true).observeSingleEvent(of: DataEventType.value, with: {(datasnapshot) in
for item in datasnapshot.children.allObjects as! [DataSnapshot]{
self.chatrooms.removeAll()
if let chatroomdic = item.value as? [String:AnyObject]{
let chatModel = ChatModel(JSON: chatroomdic)
self.chatrooms.append(chatModel!)
}
}
self.tableview.reloadData()
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.chatrooms.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RowCell", for:indexPath) as! CustomCell
var destinationUid: String?
for item in chatrooms[indexPath.row].users{
if(item.key != self.uid){
destinationUid = item.key
}
}
Database.database().reference().child("users").child(destinationUid!).observeSingleEvent(of: DataEventType.value, with: { (datasnapshot) in
let userModel = UserModel()
userModel.setValuesForKeys(datasnapshot.value as! [String:AnyObject])
cell.label_title.text = userModel.userName
let url = URL(string: userModel.profileImageUrl!)
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, err) in
DispatchQueue.main.sync {
cell.imageview.image = UIImage(data: data!)
cell.imageview.layer.cornerRadius = cell.imageview.frame.width/2
cell.imageview.layer.masksToBounds = true
}
}).resume()
let lastMessagekey = self.chatrooms[indexPath.row].comments.keys.sorted(){$0>$1}
cell.label_lastmessage.text = self.chatrooms[indexPath.row].comments[lastMessagekey[0]]?.message
})
return cell
}
override func viewDidAppear(_ animated: Bool) {
viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
class CustomCell: UITableViewCell {
@IBOutlet weak var label_lastmessage: UILabel!
@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var label_title: UILabel!
}
'Swift > firebase' 카테고리의 다른 글
스위프트 파이어베이스 카카오톡 #17~ 애플 개발자 계정 필요 swift firebase (내용없음) (0) | 2018.09.18 |
---|---|
스위프트 파이어베이스 카카오톡 #16 [ 메세지 보낸 시간 TimeStamp ] (0) | 2018.09.18 |
스위프트 파이어베이스 카카오톡 #14 [ 대화방 리스트(Chat Room List) 만들기 ] swift firebase (0) | 2018.09.16 |
스위프트 파이어베이스 카카오톡 #13 [ 말풍선 Chat Bubble2 만들기 ] swift firebase (0) | 2018.09.15 |
스위프트 파이어베이스 카카오톡 #12 [ 말풍선 Chat Bubble 만들기 ] swift firebase (0) | 2018.09.14 |