Ios 在swift中从表视图和服务器中删除行
我正试图从表视图中删除一行,我想从服务器中删除该行,以使用localhost作为我的服务器 我的删除URL位于id位置,我必须发送id,我正在通过表视图的did select获取该id,我正在通过该URL,并且在执行此过程时无法从服务器中删除 请帮助我,我的代码有什么变化吗Ios 在swift中从表视图和服务器中删除行,ios,swift,xcode,uitableview,localhost,Ios,Swift,Xcode,Uitableview,Localhost,我正试图从表视图中删除一行,我想从服务器中删除该行,以使用localhost作为我的服务器 我的删除URL位于id位置,我必须发送id,我正在通过表视图的did select获取该id,我正在通过该URL,并且在执行此过程时无法从服务器中删除 请帮助我,我的代码有什么变化吗 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: I
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// getting id //
id1 = "\(arrdata[indexPath.row].id)"
print(id1)
self.arrdata.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
let url = URL(string: "http://localhost:8080/hub/business/fun/=%@",id1 as CVarArg)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do{if error == nil{
self.arrdata = try JSONDecoder().decode([jsonstruct3].self, from: data!)
for mainarr in self.arrdata{
// print(mainarr.name,":",mainarr.capital,":",mainarr.alpha3Code)
// print(data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
catch
{
print("Error in get json data")
print(error)
}
}.resume()
tableView.reloadData()
tableView.endUpdates()
}
}
这是答案,请按照它 首先,要从应用程序端删除记录,您需要 从数组中删除它,还需要从 桌面视图 要从服务器端删除记录,需要调用API以 让服务器知道将删除哪个数据服务器 我检查了您的代码,发现从应用程序端删除记录似乎是正确的,但在调用API时-我想您需要向服务器传递更多信息。您可能希望传递头、HTTP方法、参数等。您可以使用URLRequest完成所有这些操作。您刚刚在代码中传递了URL,因此请尝试传递URL请求,而不是直接传递URL 在回答之后,请尝试下面的代码来检查是否正确 您从服务器端得到的信息
谢谢,请分享类似的源代码,这样我可以根据我的要求进行修改,或者你可以分享任何与此相关的帖子this@vamsi我更新了我的答案,请现在查看。关于需要传递给服务器的头、参数和HTTP方法,您可以向开发此API的后端开发人员询问。@vamsi,我根据您的要求更新了我的全部答案。参数标签的字符串:,_u:'不匹配任何可用的重载我在这一行中遇到此错误我无法确定它是什么类型的错误,它来自这一行。guard let url=URLstring:as CVarArg else{return}@Gopali很抱歉,我花了太长时间尝试了您的方案,但不起作用,您将我的动态id添加到我的url中,我在这个方案中尝试以这种方式发布我的url,如果让url=URLstring:{
if let url = URL(string: "Your API URL here") {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST" //Can be any as "GET", "PUT"
urlRequest.addValue("Bearer \("token")", forHTTPHeaderField: "Authorization") //Based on your back-end requirements.
let params = ["id": "5"] //Based on your back-end requirements.
urlRequest.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if error == nil, let jsonData = data {
do {
if let jsonResponse = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
print(jsonResponse)
}
} catch {
print("\(error.localizedDescription)")
}
}
}.resume()
}