SwiftyJSON响应中的for循环

时间:2019-05-25 07:46:45

标签: ios arrays swift

我有来自Alamofire的JSON响应。我想用JSON填充我的tableviews卷,但只显示一个卷。其他JSON不会加载到自定义tableView单元格中

  If segmentedControl.selectedSegmentIndex == 0 {

            cell.textLabel?.text =    tableViewData[indexPath.section].sectionData[indexPath.row - 1]
            cell.currentRideDriverPassengerName.text =   currentDriverRidePassaengerDataModel.name
            cell.currentRideDriverPassengerLocation.text =   currentDriverRidePassaengerDataModel.location
            cell.currentRideDriverPassengerDestination.text = currentDriverRidePassaengerDataModel.destination
            cell.currentRideDriverPassengerPrice.text = String(currentDriverRidePassaengerDataModel.price)
            cell.currentRideDriverPassengerSeat.text = String(currentDriverRidePassaengerDataModel.seat)
            cell.currentRideDriverPassengerDistance.text = String(currentDriverRidePassaengerDataModel.distance)
            cell.currentRideDriverPassengerImage.sd_setImage(with: URL(string: currentDriverRidePassaengerDataModel.image))

        }else if segmentedControl.selectedSegmentIndex == 1 {
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: "cell")  else {return UITableViewCell()}
            cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
        }
        return cell

    }

    }

    func getCurrentRideData(url: String) {
    Alamofire.request(url, method: .get).responseJSON {
        response in
            if response.result.isSuccess {
                print("Sucess Got the Current Ride Data")

                let currentRideJSON : JSON =   JSON(response.result.value!)
                let currentDriverPassenger : JSON = JSON(response.result.value!)
                print(currentRideJSON)
                print("This is passager\(currentDriverPassenger)")
                self.updateCurrentRideData(json: currentRideJSON)
                self.uodateCurrentRideDriverPassangerData(json: currentDriverPassenger)
            }else {
                print("error)")
            }
      }
     }


     func uodateCurrentRideDriverPassangerData(json : JSON) {
     currentDriverRidePassaengerDataModel.image = json["ride"][0] ["riders"][0]["image"].stringValue
    currentDriverRidePassaengerDataModel.name = json["ride"][0]["riders"][0]["name"].stringValue
    currentDriverRidePassaengerDataModel.location = json["ride"][0]["riders"][0]["startLocation"].stringValue
    currentDriverRidePassaengerDataModel.destination = json["ride"][0]["riders"][0]["stopLocation"].stringValue
    currentDriverRidePassaengerDataModel.price = json["ride"][0]["rider"][0]["price"].intValue
    currentDriverRidePassaengerDataModel.seat = json["ride"][0]["riders"][0]["seatNumber"].intValue
    currentDriverRidePassaengerDataModel.distance = json["ride"][0]["riders"][0]["distance"].intValue

    }

我希望我的JSON填充我的表卷而不只是一个卷

1 个答案:

答案 0 :(得分:0)

尝试一下

 func uodateCurrentRideDriverPassangerData(json : JSON) 
 {

   for i in 0...json.count-1
   {
     currentDriverRidePassaengerDataModel.image = json["ride"][i] ["riders"][i]["image"].stringValue
     currentDriverRidePassaengerDataModel.name = json["ride"][i]["riders"][i]["name"].stringValue
     currentDriverRidePassaengerDataModel.location = json["ride"][i]["riders"][i]["startLocation"].stringValue
     currentDriverRidePassaengerDataModel.destination = json["ride"][i]["riders"][i]["stopLocation"].stringValue
     currentDriverRidePassaengerDataModel.price = json["ride"][i]["rider"][i]["price"].intValue
     currentDriverRidePassaengerDataModel.seat = json["ride"][i]["riders"][i]["seatNumber"].intValue
     currentDriverRidePassaengerDataModel.distance = json["ride"][i]["riders"][i]["distance"].intValue
   }
}
相关问题