2017-07-18 4 views
0

(Moltin.com)SDKを使用して電子商取引アプリケーションを作成していますが、ドキュメントに示されているようにすべてを設定しましたが、今はテーブルビューで単一の製品の複数イメージを読み込む必要がありますカスタムセル、Iは、以下に示すコードを設定し、私は得ることができるすべては、私のアプリは、他の画像は、コントローラのコードを表示ロード無視する単一の画像は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はどこにありますか? –

+0

@MikeAlter、私は自分の関数内の行のためにセル内のイメージビューをコールしているのがわかります(コレクション辞書を設定してください)。 – Omar

答えて

0

以下のコードでは、イメージ配列(firstObject)から常に1つの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によってイメージ配列内のすべてのイメージを取得する必要があります。コールcellForRowで、この方法は、ちょうど第二のparamに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) 

} 

:たとえば

は、このような方法に新しいパラメータを追加します。 しかし、1つのセルに複数のイメージを表示したい場合は、imageViewsをカスタムセルに追加するか、UICollectionViewを使用する必要があります。 私があなたを理解できない場合は、私にpingしてください。

+0

私はあなたの応答に満足していますMyCell内のコードはうまくいきます。なぜなら私のアプリケーション内でコレクションと製品を読み込むために同じ関数を使用しているからです。 (文字列:Any)!,成功:{(レスポンス) - >無効になります self.productImages = response ?{"result"} NSArray self.tableview?.reloadData() }){(レスポンス、エラー) - >無効にする print( "何か問題があった...") }}}} }} – Omar

関連する問題