2017-10-11 3 views
0

Das Problem ist, dass wenn ich meine iOS App starte die Sammelansicht zeigt aber kein Bild angezeigt wird. Ich habe einen Ordner unter Info.plist hinzugefügt, wo meine Bilder 1.jpg und 2.jpg heißen.Meine UICollectionView zeigt keine Bilder an

let array:[String] = ["1","2"] 

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell 
    cell.imag.image = UIImage(named: array[indexPath.row] + ".jpg") 

    return cell 
} 
+0

Sie müssen debug: 'UIImage (genannt: array [indexPath.row] + ".jpg") 'ist Null? Sie sollten stattdessen 'UIImage (named: array [indexPath.row])' 'verwenden. Was ist der Rahmen von 'cell.imag'? – Larme

Antwort

1

Ja Ihr Code sollte wie sein diese

let array:[String] = ["1","2"] 

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell 
    cell.imageView?.image = UIImage(named: array[indexPath.row]) 

    return cell 
} 
+0

Mein xcode hat wahrscheinlich ein Problem mit der CollectionView-Klasse, alles funktioniert, aber CollectionView startet nicht im Simulator. Ich weiß nicht genau, was los ist – motivated

1

dieses Versuchen

cell.imag.image = UIImage(named: "\(array[indexPath.row]).jpg") 
+0

Leider funktioniert es auch nicht. Ich habe versucht, Hintergrund zur Bildansicht einzustellen und alles ist in Ordnung, aber ist ein Problem mit Bildern;/Dies zeigt keine Bilder;/ – motivated

+0

haben Sie versucht, mit URL zuzugreifen, lassen Sie BildURL = NSBundle.mainBundle(). URLForResource (" image ", withExtension:" png ") lass image = UIImage (contentsOfFile: imageURL! .path!) –

+0

Oder verschiebe dein Bild in" Assets.xcassets "und versuche es dann –