         }
     }
     var chatroomDoc: CBLDocument?
+
+    var editingMessage: CBLDocument? {
+        didSet {
+            if let doc = editingMessage {
+                self.textField.text = doc["message"] as? String
+                self.textField.becomeFirstResponder()
+            } else {
+                self.textField.text = ""
+            }
+        }
+    }
 
     func configureView() {
         if let roomId = self.chatroomId,

            return
        }

        let app = UIApplication.sharedApplication().delegate as! AppDelegate

        let message = text!

        let properties = [
            "type": "message",


        let doc = database.createDocument()
        try! doc.putProperties(properties)
    }

    func sendEditedMessage(doc: CBLDocument, message: String) {
        if var newProps = doc.properties as? [String: AnyObject] {
            newProps["message"] = message
            try! doc.putProperties(newProps)

            editingMessage = nil
        }
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
