2016-05-18 5 views
-1

Ich habe 3 Bild-URL von einer API angefordert. und ich möchte 3 Bilder von URL-Bildern anzeigen, die in der Collection View in einer Zelle erhalten wurden. Ich bin neu in schnellen ProgrammierungAnzeige von 3 Bildern von der URL zur Zelle von Collection View Swift

hier ist mein Code

class MainCollectionViewController: UICollectionViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Alamofire.request(.POST, "URL").responseJSON { response in 

     if let value = response.result.value { 

      let json = JSON(value) 

      let data = json["data"].arrayValue 

      for datas in data { 

       let result = datas["image"].stringValue 

       print(result) 

      } 
     } 
    } 
} 

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
} 

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 3 
} 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 

     return cell 
    } 
} 

mein Bild Ansicht auf mainCollectionViewCell namens mainImageView

+0

hier angezeigt Bild-URL rechts druckt führen? –

+0

yup, es wird Bild URL –

+1

drucken Sie sollten google alle allgemeinen Tutorial für Web-Anfrage. die meisten von ihnen tun ähnliche Sachen, die Sie wollen – Shubhank

Antwort

0

Ich löste es. hier ist mein Code

class MainCollectionViewController: UICollectionViewController { 


@IBOutlet var mainCollectionView: UICollectionView! 
var url = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Alamofire.request(.POST, "URL").responseJSON { response in 

     if let value = response.result.value { 

      let json = JSON(value) 

      let data = json["data"].arrayValue 

      for datas in data { 

       self.url.append(datas["image"].stringValue) 
      } 
      self.mainCollectionView.reloadData() 
     } 
    } 
} 
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
} 

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return url.count 
} 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 



       let imageUrl:NSURL? = NSURL(string: url[indexPath.row]) 

       if let url = imageUrl { 

        cell.mainImageView.sd_setImageWithURL(url) 

     } 
     return cell 
} 

i SDWebImages bin mit dem Bild

0

schreiben etwas wie unten in Ihrem Code,

class MainCollectionViewController: UICollectionViewController { 

var myImageArray : NSMutableArray = NSMutableArray() 

override func viewDidLoad() { 
super.viewDidLoad() 

Alamofire.request(.POST, "URL").responseJSON { response in 

    if let value = response.result.value { 

     let json = JSON(value) 

     let data = json["data"].arrayValue 

     for datas in data { 

      let result = datas["image"].stringValue 

      self.myImageArray.addObject(result) 
      print(result) 

     } 
    } 
} 
} 

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
return 1 
} 

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
return self.myImageArray.count 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 

    let url = NSURL(string: self.myImageArray.objectAtIndex(indexPath.row) as String) 
    let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check 
    cell.mainImageView.image = UIImage(data: data!) 

    return cell 
} 
} 

Hope this wird dir helfen :)

+0

Sie können auch Drittanbieter-Bibliothek wie SDWebImage verwenden, um Bild von URL zu laden. –

+0

es heißt "AnyObject" ist nicht konvertierbar zu "String"; Wolltest du 'wie!' niedergeschlagen werden? in Zeile "let url = NSURL' dann füge ich '!' nach als. dann ist der Build erfolgreich, aber es wird SIGABRT-Fehler ausgegeben –

+0

Ist Ihr mainImageView in Ihrer Zelle verkabelt oder mit ImageView verbunden? und überprüfen Sie, ob Sie einen Haltepunkt in der URL-, Daten- und Bildansicht einfügen. Und drucken Sie Url in Zelle, um zu überprüfen, ob Array es hat oder nicht? –

Verwandte Themen