2017-10-11 114 views
0

問題是,當我啓動我的iOS應用程序集合視圖顯示,但沒有顯示任何圖像。我在Info.plist下添加了一個文件夾,其中我的圖片被命名爲1.jpg和2.jpg。我的UICollectionView沒有顯示圖像

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

你需要調試:'的UIImage(命名爲:數組[indexPath.row] + 「.JPG」 )'是零?您應該使用'UIImage(named:array [indexPath.row])'代替。此外,「cell.imag」的框架是什麼? – Larme

回答

1

是你的代碼應該是這樣的

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

我的xcode可能有collectionview類的問題,一切正常,但collectionview並沒有在模擬器中開始。我完全不知道發生了什麼 – motivated

1

試試這個

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

不幸的是,它也行不通。我嘗試將背景設置爲圖像視圖,並且一切正常,但是對圖片有問題; /這不顯示任何圖片;/ – motivated

+0

您嘗試使用URL訪問let imageURL = NSBundle.mainBundle()。URLForResource(「圖像「,withExtension:」PNG「) 讓圖像= UIImage(contentsOfFile:imageURL!.path!) –

+0

或移動您的圖像在」Assets.xcassets「,並嘗試 –