2016-09-15 45 views
-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() 
} 
+0

你提的问题是非常不清楚。要么描述你一步一步做了什么,要么添加一些你写的代码。 – Aks

+0

我添加了代码示例。那是怎么回事? – poorni

回答

1

我在上面的代码片段中看不到任何代码,它使用新行更新表格视图。你需要按照你的tableview中添加一个新行的步骤是

  1. 更新您的数据源(数组,字典 - 无论是用来填充表)

  2. 插入一行在你的tableview通过使用https://developer.apple.com/reference/uikit/uitableview/1614879-insertrows一方或通过重新加载实现代码如下(前者是首选)

,你显示警报这可以做的权利。

如果你正面临其他一些问题,如不反映在表中的数据,你可能要添加更多详细的问题