问题是,当我启动iOS应用程序时,集合视图会显示,但不会显示任何图像。我在Info.plist下添加了一个文件夹,我的图片名为1.jpg和2.jpg。
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 :(得分: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
}
答案 1 :(得分:1)
试试这个
cell.imag.image = UIImage(named: "\(array[indexPath.row]).jpg")