【发布时间】: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