I have a table view that I am trying to make reactive. I have it working using Swift.
Following is the RxSwift code
viewModel.getDetailMessages().asObservable().bind(to: messageTableView.rx.items){ tableView, row, element in
let indexPath = IndexPath(row: row, section: 0)
print("The size is", InboxData.sharedInstance.inboxdata[messageIndexPath.row].messageDetail.count)
print("The size is element", element)
if !element.isSender{
let cell = tableView.dequeueReusableCell(withIdentifier: "recieverCell", for: indexPath) as! RecieverMessageTableViewCell
cell.message.text = element.messageText
cell.message.preferredMaxLayoutWidth = 287
cell.message.layer.cornerRadius = 8
cell.message.layer.masksToBounds = true
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "senderCell", for: indexPath) as! SenderMessageTableViewCell
cell.senderMessage.text = element.messageText
cell.senderMessage.layer.cornerRadius = 8
cell.senderMessage.preferredMaxLayoutWidth = 287
cell.senderMessage.layer.masksToBounds = true
return cell
}
}
Following is my view model
class MessagwViewModel{
func getDetailMessages() -> Observable<[Message]> {
return Observable.just(InboxData.sharedInstance.inboxdata[messageIndexPath.row].messageDetail)
}
}
And this is the message data class
class MessageData {
static let sharedInstance = MessageData()
var message1 = Message(profilePicture: "Taylor Ward", artistName: "Taylor Ward", messageText: "Hi How are you?", isSender: true)
var messages = [Message]()
private init() {
loadMessages()
}
func loadMessages() {
messages.removeAll()
messages.append(message1)
}
}
On Click of a button I am trying to append data to the Message array as follows
sendButton.rx.tap.bind{ value in
InboxData.sharedInstance.inboxdata[messageIndexPath.row].messageDetail.append(Message(profilePicture: "", artistName: "", messageText: self.messageTextView.text, isSender: true))
self.messageTableView.reloadData()
self.messageTableView?.scrollToBottom()
print("Button clicked")
}
The count of the array increases every time I append a new element . But the table view does not reload even though the data source has the latest value
Can anyone one point out what the problem is? Thank you.