我的UICollectionView没有显示图像

时间:2017-10-11 15:48:25

标签: ios swift uicollectionview

问题是,当我启动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
}

2 个答案:

答案 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")