问题描述
我使用了两个 API,一个用于获取用户添加的汽车,第二个用于(添加新车)将汽车详细信息提交到我的后端。当我打开汽车详细信息的视图时,所有 11 个汽车详细信息都被正确获取。
但是当我通过单击添加圆形按钮提交汽车详细信息(添加新车)并获取添加汽车的详细信息时,它会在前端表视图中显示添加了 22 辆车,但现在在 API 的后端。
这是一些屏幕截图。
- 在添加新车之前
- 添加新车后
这是相同的代码。
func responsedatavehicle(){
let emailid = defaults.string(forKey: "email")
var request = URLRequest(url: URL(string: "http://www.example.com/getMyVehicleInfo?email=\(emailid!)")!)
print("Email in resposfunction \(emailid!)")
request.httpMethod = "GET"
request.addValue("application/x-www-form-urlencoded",forHTTPHeaderField: "Content-Type")
request.addValue("application/json",forHTTPHeaderField: "Accept")
do{
// let postString = "?mobile="+ph!
print("mobno is inside getdata \(emailid!)")
// //print(postString)
// request.httpBody = postString.data(using: .utf8)
APESuperHUD.show(style: .loadingIndicator(type: .standard),title: nil,message: "Loading...")
//self.activityIndicator("Please Wait")
}catch let error{
//print(error.localizedDescription)
return
}
// self.activityIndicator("Please Wait")
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data,response,error in
if error != nil
{
return
}
let responseString = Nsstring(data: data!,encoding: String.Encoding.utf8.rawValue)
do{
let json = try JSONSerialization.jsonObject(with: data!,options: .mutableContainers) as? NSDictionary
if let parseJSON = json{
print(parseJSON)
if let mobile = parseJSON["error"] as! Int? {
let msg = parseJSON["message"] as! String?
if (mobile == 0){
//print(userId)
dispatchQueue.main.async {
// self.showGlobalAlertwithMessage(msg as! Nsstring)
if let heroeArray = json!.value(forKey: "content") as? Array<[String:Any]> {
//looping through all the elements
self.labelArray.append(contentsOf: heroeArray)
}
APESuperHUD.dismissAll(animated: true)
// self.effectView.removeFromSuperview()
//print(self.labelArray.count)
for i in 0..<self.labelArray.count{
if let heroeDict = self.labelArray[i] as? NSDictionary{
// let stationname = heroeDict.value(forKey: "charging_station") as! String
let brandname = heroeDict.value(forKey: "brand_name") as! String
let modelname = heroeDict.value(forKey: "car_name") as! String
let accharger = heroeDict.value(forKey: "ac_charger") as! String
let dccharger = heroeDict.value(forKey: "dc_charger") as! String
let vehiclenumber = heroeDict.value(forKey: "vehicle_registration_number") as! String
let imageurl = heroeDict.value(forKey: "vehicle_photo") as! String
self.brandnamearray.append(brandname)
self.modelnamearray.append(modelname)
self.acchargerarray.append(accharger)
self.dcchargerarray.append(dccharger)
self.imageurlarray.append(imageurl)
self.vehiclenoarray.append(vehiclenumber)
print(self.imageurlarray)
}
}
self.myTableView.reloadWithAnimation()
}
}else {
dispatchQueue.main.async {
//self.showGlobalAlertwithMessage(msg as! Nsstring)
dispatchQueue.main.async {
APESuperHUD.dismissAll(animated: true)
// self.effectView.removeFromSuperview()
}
}
}
}
} else{
print("ERROR IN ALERT")
}
}
catch{
// self.showGlobalAlertwithMessage("Could not successfully perform this request. Please try again later")
}
}
task.resume()
}
Cellforrowat 函数
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! MyVehicleTableViewCell
cell.brandnamecell.text = brandnamearray[indexPath.row]
cell.modelnamecell.text = modelnamearray[indexPath.row]
cell.acchargercell.text = acchargerarray[indexPath.row]
cell.dcchargercell.text = dcchargerarray[indexPath.row]
cell.vehiclenumbercell.text = vehiclenoarray[indexPath.row]
cell.ImageView.sd_setimage(with: URL(string: imageurlarray[indexPath.row]),placeholderImage: UIImage(named: "placeholder.png"))
cell.aclabel.layer.borderColor = UIColor.darkGray.cgColor
cell.aclabel.layer.borderWidth = 1.0
cell.aclabel.layer.cornerRadius = 5
cell.aclabel.layer.masksToBounds = true
cell.dclabel.layer.borderColor = UIColor.darkGray.cgColor
cell.dclabel.layer.cornerRadius = 5
cell.dclabel.layer.masksToBounds = true
cell.dclabel.layer.borderWidth = 1.0
cell.aclabel.layer.shadowColor = UIColor.lightGray.cgColor
cell.aclabel.layer.shadowOpacity = 0.5
cell.aclabel.layer.shadowRadius = 10.0
cell.aclabel.layer.shadowOffset = .zero
cell.aclabel.layer.shadowPath = UIBezierPath(rect: cell.aclabel.bounds).cgPath
cell.aclabel.layer.shouldRasterize = true
cell.dclabel.layer.shadowColor = UIColor.lightGray.cgColor
cell.dclabel.layer.shadowOpacity = 0.5
cell.dclabel.layer.shadowRadius = 10.0
cell.dclabel.layer.shadowOffset = .zero
cell.dclabel.layer.shadowPath = UIBezierPath(rect: cell.dclabel.bounds).cgPath
cell.dclabel.layer.shouldRasterize = true
cell.backview.layer.borderColor = UIColor(red:0.92,green:0.92,blue:0.98,alpha:0.9).cgColor
cell.backview.layer.cornerRadius = 8
cell.backview.layer.masksToBounds = true
cell.backview.layer.borderWidth = 8.0
cell.shadowmodel.layer.cornerRadius = 13.0
cell.shadowmodel.layer.shadowColor = UIColor.lightGray.cgColor
cell.shadowmodel.layer.shadowOpacity = 0.5
cell.shadowmodel.layer.shadowRadius = 10.0
cell.shadowmodel.layer.shadowOffset = .zero
cell.shadowmodel.layer.shadowPath = UIBezierPath(rect: cell.shadowmodel.bounds).cgPath
cell.shadowmodel.layer.shouldRasterize = true
return cell
}
func submitcar(){
if vehiclenumberstring == ""{
SCLAlertView().showError("Error",subTitle: "Please enter vehicle number ") // Error
}
else {
let emailsubmit = defaults.string(forKey: "email")
print("email inside submit car is \(emailsubmit!)")
var request = URLRequest(url: URL(string: "http://www.example.com/apimobileapps/addVehicleInfo")!)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded",forHTTPHeaderField: "Content-Type")
request.addValue("application/json",forHTTPHeaderField: "Accept")
do{
let postString = "brand_name="+brandname+"&car_name="+modelname+"&vehicle_registration_number="+vehiclenumberstring+"&email="+emailsubmit!
print(postString)
request.httpBody = postString.data(using: .utf8)
APESuperHUD.show(style: .loadingIndicator(type: .standard),message: "Loading...")
// self.activityIndicator("Please Wait")
}catch let error{
//print(error.localizedDescription)
return
}
let task = URLSession.shared.dataTask(with: request){
(data,error) in
if (error != nil){
return
}
do{
let json = try JSONSerialization.jsonObject(with: data!,options: .mutableContainers) as? NSDictionary
if let parseJSON = json{
//print(parseJSON)
if let userId = parseJSON["error"] as! Int? {
let msg = parseJSON["message"] as! String?
if (userId == 0){
//print(userId)
dispatchQueue.main.async {
// self.showGlobalAlertwithMessage(msg as! Nsstring)
dispatchQueue.main.async {
APESuperHUD.dismissAll(animated: true); self.effectView.removeFromSuperview()
}
}
}else {
dispatchQueue.main.async {
// self.showGlobalAlertwithMessage(msg as! Nsstring)
dispatchQueue.main.async {
APESuperHUD.dismissAll(animated: true)
// self.effectView.removeFromSuperview();
}
}
}
}
} else{
dispatchQueue.main.async {
APESuperHUD.dismissAll(animated: true)
// self.effectView.removeFromSuperview();
}
}
}catch{
dispatchQueue.main.async {
APESuperHUD.dismissAll(animated: true)
// self.effectView.removeFromSuperview();
}
//print(error)
}
}
task.resume()
}
responsedatavehicle()
}
我的数组计数在提交按钮操作时在表格视图中显示重复值。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)