匹配行选择传递数据

Match row selected to pass data

我已经查看了所有这些 Swift、asp.net 和 javascript 问题。

4 5

目标:
当我 select 来自 MessageListController 聊天消息列表的消息时,我希望下一个 ChatDetailController 中打开的会话是 selected 的对话。

我在这张 iOS 图像中为我的 WatchKit 应用程序做同样的事情。 select 编辑了与 Sophia 的消息,并打开了与 Sophia 的聊天。 [![在此处输入图片描述][6]][6]

我想通过 json "message_id" 即 chatMessageId 属性。正如您在代码中看到的那样,我已经将 chatMessageIdMessageModel 传递到 ChatDetailController

ChatModel我需要通过的chatMessageId吗?或者我已经传递了我需要的数据?

Passed context: Optional(HTWatch_Extension.MessageModel(partner: "9859", nickname: "Marco", message: "Have you seen is dog?", city: "Madrid", countryBadgeImageURL: https://i.imgur.com/PJcyle7.jpg, messageListImageURL: https://i.imgur.com/PJcyle7.jpg, chatMessageId: "Tva9d2OJyWHRC1AqEfKjclRwXnlRDQ", status: "offline"))

尝试: 我是否需要将我从 ChatDetailControllerawakeWithContext 方法中解析 ChatModel 的 do-catch 块取出并放入 didSelectRowAt 的 [=] 方法中11=]?


消息列表控制器

// ...code...

var messageObject = [MessageModel]()
var chatObject = [ChatModel]()

// ...code...

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

    var messageContext = messageObject[rowIndex]
    var chatContext = chatObject[rowIndex]

    do {
        guard let fileUrl = Bundle.main.url(forResource: "Chats", withExtension: "json") else {
            print("File could not be located")
            return
        }
        let data = try Data(contentsOf: fileUrl)
        let decoder = JSONDecoder()

        let msg = try decoder.decode([ChatModel].self, from: data)
        self.chatObject = msg

    } catch let error {
        print(error)
    }

    messageContext.chatMessageId = (chatObject as AnyObject).filter { (dictionaryTemp:[String:String]) -> Bool in
        return dictionaryTemp["message_id"] == chatContext.chatMessageId
    } 

    // WatchKit's model presentation method.
    presentController(withName: "ChatDetailController", context: messageContext)
}

如果我没理解错的话,您的 Chat.json 将对所有消息 ID 进行聊天。 Select 消息 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"
    ]
]

你做了 Select 索引处的行

    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)
        }