-2
我是swift編程的新手。我創建了一個簡單的web服務來與swift應用程序連接。然後,我將JSON數據作爲列表檢索到表視圖並將數據插入到Web服務中。現在我想更新表視圖中的json數據行。請任何人幫我更新Swift Table View中的JSON數據
我用這段代碼來插入數據。
@IBAction func btnInsert(sender: AnyObject) {
let companyName = txtcompanyName.text
let phoneNumber = txtphoneNumber.text
let country = txtCountry.text
let address = txtAddress.text
let email = txtEmail.text
let password = txtPassword.text
// Send HTTP POST
let myUrl = NSURL(string: "http://localhost:8888/MAMP/appconnect/userRegister.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let postString = "companyName=\(companyName!)&phoneNumber=\(phoneNumber!)&country=\(country!)&address=\(address!)&email=\(email!)&password=\(password!)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
if let parseJSON = json {
let resultValue = parseJSON["status"] as! String!
print("result: \(resultValue)")
var isUserRegistered:Bool = false
if(resultValue == "Success") {
isUserRegistered = true
}
var messageToDisplay:String = parseJSON["message"] as! String
if(!isUserRegistered) {
messageToDisplay = parseJSON["message"] as! String
}
dispatch_async(dispatch_get_main_queue(), {
//Display alert message with confirmation
let myAlert = UIAlertController(title: "Alert", message: messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){ action in
self.dismissViewControllerAnimated(true, completion: nil)
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
})
}
} catch {
print(error)
}
}
task.resume()
}
你提的問題是非常不清楚。要麼描述你一步一步做了什麼,要麼添加一些你寫的代碼。 – Aks
我添加了代碼示例。那是怎麼回事? – poorni