我有一个表格视图,我试图使其反应。我让它使用 Swift 工作。
以下是 RxSwift 代码
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
}
}
以下是我的视图模型
class MessagwViewModel{
func getDetailMessages() -> Observable<[Message]> {
return Observable.just(InboxData.sharedInstance.inboxdata[messageIndexPath.row].messageDetail)
}
}
这是消息数据类
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)
}
}
单击按钮时,我试图将数据附加到 Message 数组,如下所示
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")
}
每次我追加一个新元素时,数组的计数都会增加。但是即使数据源具有最新值,表视图也不会重新加载
谁能指出问题是什么?谢谢你。