从API加载图像

时间:2017-07-18 12:35:40

标签: ios json swift xcode moltin

我正在创建一个带有(Moltin.com)SDK的电子商务应用程序,我按照文档中的说明设置好了所有东西,但现在我需要在自定义单元格的表视图中加载单个产品的多个图像,I设置下面显示的代码,我可以获得的是单个图像我的应用程序忽略加载其他图像视图控制器代码是

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
}

我的自定义单元格代码是

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))


}

任何人都可以告诉我哪些问题不能让我获得我产品的所有图像? 我正在使用SWIFT 3,使用XCode

1 个答案:

答案 0 :(得分:0)

在下面的代码中,您始终从images array(firstObject)获取一个URL。

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)

}

如果我理解正确,你应该通过tableView的indexPath.row获取images数组中的每个图像。

例如,向这样的方法添加新参数:

{{1}}

比在cellForRow中调用此方法时,只需将indexPath.row添加到第二个参数。 但是,如果要在一个单元格中显示多个图像,则应将更多imageView添加到自定义单元格或使用UICollectionView。 如果我不明白你,请跟我打电话。