我正在使用(Moltin.com)SDK創建電子商務應用程序,我將所有內容都設置爲在文檔中顯示,但現在我需要在表格視圖中加載單個產品的多個圖像自定義單元格,我設置如下所示的代碼和所有我能得到的是一個單一的形象我的應用程序忽略加載其他圖片查看器代碼是從API加載圖像
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
你的問題不清楚,你想在單元格中顯示多個圖像? ,但imageView在哪裏? ,你得到了'(images.firstObject as!NSDictionary'那麼你如何實現這一點? –
@MikeAlter,你可以看到我在我的函數(set集合字典)中調用單元格中的圖像視圖。 – Omar