Ios CustomCell未显示两条消息swift

Ios CustomCell未显示两条消息swift,ios,swift,uitableview,Ios,Swift,Uitableview,我想显示用户消息和存储为数组的聊天机器人消息。测试以下代码后,将显示chatbot消息,但不会显示用户消息。谁能解决这个问题 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell",

我想显示用户消息和存储为数组的聊天机器人消息。测试以下代码后,将显示chatbot消息,但不会显示用户消息。谁能解决这个问题

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! TableViewCell
        cell.messages.text = messageArray[indexPath.row].userMessage
        cell.messages.text = messageArray[indexPath.row].sundayMessage
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(messageArray.count)
        return messageArray.count

    }

此处后一行替换前一行设置的整个字符串:

cell.messages.text = messageArray[indexPath.row].userMessage
cell.messages.text = messageArray[indexPath.row].sundayMessage
如果希望两条消息都位于
单元格.messages
文本中,可以执行以下操作:

let message = messageArray[indexPath.row]
cell.messages.text = "user: " + message.userMessage + "\nsunday: " + message.sundayMessage

您需要在模型中进行一些更改,以检查当前消息是否为用户类型或机器人类型。一旦你能弄明白,你就可以使用以下逻辑:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! TableViewCell

           //this is the check you need to have
           if messageArray[indexPath.row].userType == "bot"{
            cell.messages.text = messageArray[indexPath.row].sundayMessage

           }else if messageArray[indexPath.row].userType == "user"{
            cell.messages.text = messageArray[indexPath.row].userMessage
           }
            return cell
     }

这将仅显示
messageArray[indexath.row].sundayMessage
。请仔细阅读您的代码
userMessage
sundayMessage
分配给同一标签。第二个值覆盖第一个值。您正在将userMessage和chatbot message设置为同一标签。检查并将其正确添加到相应的单元格您正在覆盖的cell.messages标签上。这就是为什么你看不到userMessage和sundaymessage如果你想在一个单元格中同时显示这两条消息,那么在单元格中添加另一个标签,并设置
messageArray[indexPath.row].sundayMessage
到新创建的标签。我使用了此代码,但在聊天机器人答复后,用户消息行将更新为聊天机器人消息,下面创建了另一行聊天机器人消息,如果updateTVC==true{cell.messages.text=messageArray[indexPath.row].userMessage updateTVC=false}else{cell.messages.text=messageArray[indepath.row].sundayMessage}返回单元格我认为现在的问题是由于您用于检查的模型没有正确更新。发送聊天机器人消息和用户消息后,应更新模型,然后仅重新加载表。当用户发送消息时,表将重新加载,从聊天机器人获取消息后,表将再次重新加载。之后,第一条消息被chatbot消息替换,并显示另一条chatbot消息。我已经打印了模型,效果很好。我仍然认为,您用于从中提取数据的模型/模型数组存在一些问题,因为上面的逻辑是基于该逻辑简单呈现的。检查是否在正确的时间切换updateTVC,当第二个条件执行多次时,它似乎是正确的。当我打印返回值时,如果它是3,那么第二个值的indexpath是0,1,2。如何修复它