Search code examples
iosswiftuitableviewimessage-extension

How to pass values from one custom cell to another view controller by using global variables for iMessage?


I'm trying to develop an iMessage App and i'm not sure how to pass values from a custom cell to another view controller. I need to make sure that once the button is pressed the value from the cell is copied to the text sending space. Please help me out! Im stuck here :

This is the iMessage Code and instead of "hello world" it needs to put the label text from the cell

        let layout = MSMessageTemplateLayout()
        layout.caption = "Hello World"

        let message = MSMessage()
        message.layout = layout

        self.activeConversation?.insert(message, completionHandler: nil)

The part which has the details on the cell in a different view controller

        func configureWithContactEntry(_ contact: ContactEntry) {
        contactNameLabel.text = contact.name
        contactEmailLabel.text = contact.email ?? ""
        contactPhoneLabel.text = contact.phone ?? ""


        return cell

This is the CellForRow code

    let cell = tableView.dequeueReusableCell(withIdentifier: 
    "ContactTableViewCell", for: indexPath) as! ContactTableViewCell
    let entry = filteredContacts[(indexPath as NSIndexPath).row]
    cell.configureWithContactEntry(entry)
    cell.layoutIfNeeded()

    return cell

Would be great if anybody could help me out!


Solution

  • You can pass your cell data to other controller in your imessage extension in this way. check your cell selected override function its look like

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        print("cell selected at row \(indexPath.row)")
    }
    

    Get model data on the base of indexPath from your model object, in your case you should get from this function.

    let entry = filteredContacts[(indexPath as NSIndexPath).row]
    let name = entry.name
    

    After that create Msession object and fill data in it from your entry object.

    let session = MSSession()
            let message = MSMessage(session: session)
            let layout = MSMessageTemplateLayout()
            layout.caption = name
    
    
            message.layout = layout
    
            //save the message in the current conversation context
    
            self.savedConversation?.insert(message,
                                           completionHandler: { (err) in
    
                                            print("ERROR \(err.debugDescription)")
            })
    

    Also if you want to append image in message then do it this way.

            layout.image = UIImage(named:"image.png")!