2016-09-15 44 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一方或通過重新加載實現代碼如下(前者是首選)

,你顯示警報這可以做的權利。

如果你正面臨其他一些問題,如不反映在表中的數據,你可能要添加更多詳細的問題