【问题标题】:Load images from API从 API 加载图像
【发布时间】:2017-07-18 12:35:39
【问题描述】:

我正在使用 (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
}

我的自定义单元格代码是

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


}

谁能告诉我我无法获取产品的所有图片的问题出在哪里? 我正在使用带有 XCode 的 SWIFT 3

【问题讨论】:

  • 您的问题不清楚,您想在单元格中显示多张图片吗? ,但是 imageView 在哪里? ,你得到(images.firstObject as! NSDictionary那么你怎么能做到这一点?
  • @MikeAlter,您可以看到我正在调用单元格中的图像视图以获得我的函数中的行(设置集合字典)。

标签: ios json swift xcode moltin


【解决方案1】:

在下面的代码中,您总是从图像数组 (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)) 

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

例如向方法中添加新参数,如下所示:

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)

}

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

【讨论】:

  • 我很高兴收到您的回复,我在 MyCell 中的代码运行良好,因为我使用相同的函数在我的应用程序中加载我的收藏和产品,我认为调用此行的问题代码 ` {{{ . 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...") }`}}}}}
猜你喜欢
  • 2022-11-16
  • 1970-01-01
  • 2021-10-22
  • 2016-06-25
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
相关资源
最近更新 更多