2017-07-18 56 views
0

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

+0

你的問題不清楚,你想在單元格中顯示多個圖像? ,但imageView在哪裏? ,你得到了'(images.firstObject as!NSDictionary'那麼你如何實現這一點? –

+0

@MikeAlter,你可以看到我在我的函數(set集合字典)中調用單元格中的圖像視圖。 – Omar

回答

0

在下面的代碼中,您總是會從圖像數組(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到第二PARAM。 但是,如果你想在一個單元格中顯示多個圖像,你應該添加更多的imageViews到自定義單元格或使用UICollectionView。 只要我不明白你的話,就直接給我打電話。

+0

我很高興爲您的迴應MyCell內的我的代碼運行良好,因爲我使用相同的函數來加載我的應用程序中的集合和產品,我認爲在調用這一行代碼'{{{。 Moltin.sharedInstance()。product.listing(withParameters:productDict!.value(forKeyPath:「url.https」)as![String:Any] !,成功:{(response) - >無效 self.productImages = response (「出現錯誤...」) }'}}} }} – Omar