选择匹配行以传递数据

时间:2019-04-02 22:37:51

标签: json swift chat messaging

我研究了所有这些Swift,asp.net和javascript问题。

1 2 3 4 5

目标:
当我从MessageListController中的聊天消息列表中选择一条消息时,我希望下一个ChatDetailController中打开的会话是所选的会话。

我在此iOS图像中为WatchKit应用执行相同的操作。选择与Sophia的消息,并打开与Sophia的聊天。 [![在此处输入图片描述] [6]] [6]

我想传递json“ message_id”,即chatMessageId属性。如您在代码中所见,我已经将chatMessageIdMessageModel传递到ChatDetailController

我需要通过chatMessageId的{​​{1}}吗?还是我已经传递了所需的数据?

  

传递的上下文:可选(HTWatch_Extension.MessageModel(合作伙伴:“ 9859”,昵称:“马可”,消息:“您看到过狗吗?”,城市:“ Madrid”,countryBadgeImageURL:https://i.imgur.com/PJcyle7.jpg,messageListImageURL :https://i.imgur.com/PJcyle7.jpg,chatMessageId:“ Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ”,状态:“离线”))

尝试: 我是否需要从ChatModel的{​​{1}}方法中解析ChatModel的do-catch块中放入{{ 1}}?


MessageListController

ChatDetailController

1 个答案:

答案 0 :(得分:3)

如果我正确理解,您的Chat.json将拥有所有消息ID的聊天记录。选择消息ID行之一,并加载各自的聊天记录。

在这种情况下,您可以使用filter根据消息进行解析。假设您在这样的字典中有它。

示例:

let responseString = "{\"name\":\"Tom\"}"

if let responseData = responseString.data(using: .utf8){
    do {

    let object = try JSONSerialization.jsonObject(with:responseData , options: .allowFragments)
    print("Response Object=\(object)")
    } catch{
        print("parsing Error=\(error)")
    }
}

您可以使用类似的代码创建对象。最终对象应类似于chatDictionary

let chatDictionary = [
[
    "fromId": "zz1234skjksmsjdfwe2zz",
    "toId": "qq43922sdkfjsfmmxdfqq",
    "messageText": "Have you seen is dog?",
    "imageUrl": "https://i.imgur.com/PJcyle7.jpg",
    "message_id": "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ",
    "read": "true"
    ],
[
    "fromId": "zz1234skjksmsjdfwe2zz",
    "toId": "qq43922sdkfjsfmmxdfqq",
    "messageText": "Yes I have. It's cute.",
    "imageUrl": "https://i.imgur.com/PJcyle7.jpg",
    "message_id": "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ",
    "read": "true"
    ],
[
    "fromId": "zz1234skjksmsjdfwe2zz",
    "toId": "qq43922sdkfjsfmmxdfqq",
    "messageText": "I want to get a pet too.",
    "imageUrl": "https://i.imgur.com/PJcyle7.jpg",
    "message_id": "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ1",
    "read": "true"
    ]
]

您确实选择了索引行

    override func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) {

            let message = messageObjects[rowIndex]
            // Create a chat object Dictionary, parse it before you pass it to the detail View Controller , if you have the chat.json.
// I have used 'Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ', but here you can your message id property to make it dynamic
            message.chatObjects= chatDictionary.filter { (dictionaryTemp:[String : String]) -> Bool in
            return dictionaryTemp["message_id"] == "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ"
        }
            presentController(withName: "ChatDetailController", context: message)
        }