简体   繁体   English

从API加载图像

[英]Load images from API

I'm creating an e-commerce app with (Moltin.com) SDK, I set every thing well as it shown in the documentation but now I need to load multi images of single product in table view with custom cell, I set the shown code below and all I can get is a single image my app ignore load the other images view controller code is 我正在使用(Moltin.com)SDK创建一个电子商务应用,我对其设置进行了很好的设置,但现在我需要使用自定义单元格在表格视图中加载单个产品的多张图片,下面的代码,我能得到的是我应用程序忽略加载的单个图像,其他图像视图控制器代码是

class vc: UIViewController , UITableViewDelegate, UITableViewDataSource {

var productDict:NSDictionary?

@IBOutlet weak var tableview: UITableView!
fileprivate let MY_CELL_REUSE_IDENTIFIER = "MyCell"
fileprivate var productImages:NSArray?

override func viewDidLoad() {
    super.viewDidLoad()

    tableview.delegate = self
    tableview.dataSource = self



     Moltin.sharedInstance().product.listing(withParameters: productDict!.value(forKeyPath: "url.https") as! [String : Any]!, success: { (response) -> Void in
        self.productImages = response?["result"] as? NSArray
        self.tableview?.reloadData()
    }) { (response, error) -> Void in

        print("Something went wrong...")
    }
}

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if productImages != nil {
        return productImages!.count
    }

    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: MY_CELL_REUSE_IDENTIFIER, for: indexPath) as! MyCell
    let row = (indexPath as NSIndexPath).row
    let collectionDictionary = productImages?.object(at: row) as! NSDictionary
    cell.setCollectionDictionary(collectionDictionary)
    return cell
}

and my custom cell code is 我的自定义单元格代码是

class MyCell: UITableViewCell {

@IBOutlet weak var myImage: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

func setCollectionDictionary(_ dict: NSDictionary) {
    // Set up the cell based on the values of the dictionary that we've been passed


    // Extract image URL and set that too...
    var imageUrl = ""

    if let images = dict.value(forKey: "images") as? NSArray {
        if (images.firstObject != nil) {
            imageUrl = (images.firstObject as! NSDictionary).value(forKeyPath: "url.https") as! String
        }
    }

    myImage?.sd_setImage(with: URL(string: imageUrl))


}

Can anyone show me where is the issue that doesn't let me get all the images of my product? 谁能告诉我问题出在哪里,让我无法获得产品的所有图像? I'm using SWIFT 3, with XCode 我正在使用带有XCode的SWIFT 3

In the code below you are always getting one URL from images array (firstObject). 在下面的代码中,您始终会从images数组(firstObject)获得一个URL。

if let images = dict.value(forKey: "images") as? NSArray {
    if (images.firstObject != nil) {
        imageUrl = (images.firstObject as! NSDictionary).value(forKeyPath: "url.https") as! String
    }
}

myImage?.sd_setImage(with: URL(string: imageUrl)) 

If I understand correctly you should get every image in images array by the indexPath.row of your tableView. 如果我理解正确,则应该通过tableView的indexPath.row获取images数组中的每个图像。

For example add new parameter to method like this: 例如,向这样的方法添加新参数:

func setCollection(with dict: NSDictionary, and index: Int) {
// Set up the cell based on the values of the dictionary that we've been passed


// Extract image URL and set that too...
var imageUrlString = ""

if let images = dict.value(forKey: "images") as? Array<NSDictionary>, images.count >= index {
    guard let lImageUrlString = images[index]["url.https"] else { return }
    imageUrlString = lImageUrlString
}

guard let imageURL = URL(string: imageUrl) else { return }
myImage?.sd_setImage(with: imageURL)

}

Than when call this method in cellForRow just add indexPath.row to the second param. 比在cellForRow中调用此方法时,只需将indexPath.row添加到第二个参数。 But if you want show multiple images in one cell you should add more imageViews to the custom cell or use UICollectionView. 但是,如果要在一个单元格中显示多个图像,则应向自定义单元格中添加更多imageViews或使用UICollectionView。 Just ping me if I don't understand you clear. 如果我不明白您的意思,请ping我一下。

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

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