Created
January 3, 2020 05:16
-
-
Save Krishna21Soni13/54dfa4c5a2fd7d41539901d2f1658d77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ChatDetailsTableView: UITableView, UITableViewDelegate, UITableViewDataSource { | |
private var messageViewModel: MessageViewModel = MessageViewModel() | |
var nickName: String? | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
configuartionTableView() | |
configureViewModel() | |
} | |
private func configuartionTableView() { | |
self.register(UINib(nibName: "MessageSendTableViewCell", bundle: nil), forCellReuseIdentifier: "MessageSendTableViewCell") | |
self.register(UINib(nibName: "MessageTableViewCell", bundle: nil), forCellReuseIdentifier: "MessageTableViewCell") | |
self.dataSource = self | |
self.delegate = self | |
} | |
private func configureViewModel() { | |
messageViewModel.arrMessage.subscribe { [weak self] (result: [Message]) in | |
guard let self = self else { | |
return | |
} | |
self.reloadData() | |
self.scrollToBottom(animated: false) | |
} | |
messageViewModel.getMessagesFromServer() | |
} | |
} | |
// MARK: - Table view data source | |
extension ChatDetailsTableView { | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let message: Message = messageViewModel.arrMessage.value[indexPath.row] | |
if message.nickname == nickName { | |
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MessageSendTableViewCell") as? MessageSendTableViewCell else { | |
return UITableView.emptyCell() | |
} | |
cell.configureCell(message) | |
return cell | |
} | |
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MessageTableViewCell") as? MessageTableViewCell else { | |
return UITableView.emptyCell() | |
} | |
cell.configureCell(message) | |
return cell | |
} | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return messageViewModel.arrMessage.value.count | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment