简体   繁体   English

请求 Alamofire

[英]Request with Alamofire

I'm starting on swift and I'm trying to bring a list of cars with alamofire but it is not bringing The code executes without throwing errors but not the list I see a blank table view我从 swift 开始,我正在尝试使用 alamofire 带来汽车列表,但它没有带来代码执行时不会抛出错误,但不是列表我看到一个空白表视图

https://uploaddeimagens.com.br/imagens/simulator_screen_shot_-_iphone_8_-_2019-10-20_at_16-48-23-png https://uploaddeimagens.com.br/imagens/simulator_screen_shot_-_iphone_8_-_2019-10-20_at_16-48-23-png

(sorry... editing with the write code) (对不起...用写代码编辑)

My classes我的课程

struct HotelData: Codable  {
    let results: [Results]
}

struct Results: Codable  {
    let smallDescription: String
    let price: Price
    let gallery: [ImageHotel]
    let name: String
    let address: Address

    var getRandonImage: ImageHotel {
        let index = Int(arc4random_uniform(UInt32(gallery.count)))
        return gallery[index]
    }
}

============== ===============

My manager我的经理

class func getHotels(onComplete: @escaping (HotelData?) -> Void) {
        AF.request(path).responseJSON { (response) in
            guard let jsonData = response.data else { return }
            guard let hotelData = try? JSONDecoder().decode(HotelData.self, from: jsonData)
                else {
                    onComplete(nil)
                    return
            }
            onComplete(hotelData)
            return
        }
    }
}

============== ===============

Cell细胞

func prepareCell(with hotel: Results){
        lbName.text = hotel.name
        lbSmallDescription.text = hotel.smallDescription
        lbPrice.text = "R$ \(hotel.price.amount)"
        lbAdress.text = hotel.address.city
    }

============== ===============

TableView class HotelTableViewController: UITableViewController { TableView class HotelTableViewController:UITableViewController {

            var hotels: [Results] = []
            let hotelManager = HotelManager()

            override func viewDidLoad() {
                super.viewDidLoad()
                loadHotels()
            }


            override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)

            }


            override func numberOfSections(in tableView: UITableView) -> Int {
                return 1
            }


            override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return hotels.count

            }


            override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HotelTableViewCell
                let hotel = hotels[indexPath.row]
                cell.prepareCell(with: hotel)
                return cell
                }


            func loadHotels() {
                HotelManager.getHotels { (hotelData) in
                    if let hotelData = hotelData {
                        self.hotels += hotelData.results
                    }
                }
            }

        }

Your direct issue is that you need to call reloadData() if you want your UITableView to look at your downloaded data and load the appropriate cells.您的直接问题是,如果您希望UITableView查看下载的数据并加载相应的单元格,则需要调用reloadData() You can put in the completion handler of your network call, or in a didSet on your hotels property.您可以放入网络调用的完成处理程序,或放入hotels属性的didSet中。

Additionally, you shouldn't use responseJSON for Decodable types, it's redundant.此外,您不应该将responseJSON用于Decodable类型,它是多余的。 Instead you should use responseDecodable and pass the type you want to parse: responseDecodable(of: HotelData.self) { response in... } .相反,您应该使用responseDecodable并传递您要解析的类型: responseDecodable(of: HotelData.self) { response in... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM