2017-10-11 115 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“,并尝试 –