-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