【发布时间】:2020-05-22 05:08:42
【问题描述】:
我要制作一张图片幻灯片。所以我使用了 CollectionView。
但我看不到图像。有什么问题?
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var imageArray = [UIImage(named: "swipe1"),
UIImage(named: "swipe2"),
UIImage(named: "swipe3"),
UIImage(named: "swipe4"),
UIImage(named: "swipe5")]
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? DataCollectionViewCell
cell?.img.image = imageArray[indexPath.row]
return cell!
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = collectionView.frame.size
return CGSize(width: size.width, height: size.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
DataCollectionViewCell.swift
import UIKit
class DataCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var img: UIImageView!
}
故事板
CollectionView
单元格
内容视图
img
按钮
查看
图片资源是正确的。您已经完成了测试。
我怎么会出现这个问题??
有没有人和我一样的问题解决过?告诉我你是怎么解决的。
【问题讨论】:
-
你为collectionView设置了dataSource和delegate了吗?
-
@ThePedestrian 编码后想了想,所以最后设置了。但结果是一样的。
-
imageView的底部约束在哪里?
-
@PGDev 底部没有设置图像约束,因为有一个按钮。
-
另外,您指定的“swipe1”和其他图像是否存在于资产中?
标签: ios swift image uicollectionview