[英]Cell not display after scroll in collection view - swift 2.1
主控制器
class MainController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
let reUseCellName = "imgCell"
var counter = 1
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reUseCellName, forIndexPath:indexPath) as! CellClass
cell.imageView.image = UIImage(named: "\(counter)")
cell.imgName = counter
counter++
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let newView = segue.destinationViewController as! showImage
let cell = sender as! CellClass
newView.imgNo = cell.imgName
}
我认为这是您如何使用它们的方式,尝试使用图像定义数组:
var arrayOfImages = ["1","2","3","4"]
并在cellForItemAtIndexPath
使用它:
cell.imageView.image = UIImage(named: arrayOfImages[indexPath.row])
确保将DataSoruce
和Delegate
for collectionView从连接检查器连接到视图控制器中,以加载数据,并确保放置正确的图像名称。
使用以下代码。 无法显示图片的原因是您未提及图片扩展名为jpg或png的图片...您的计数器值为“ 1”,因此应用无法加载没有扩展名png或jpg的图片名称1 :
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reUseCellName, forIndexPath:indexPath) as! CellClass
cell.imageView.image = UIImage(named: "Complete image name with .png/.jpg")
cell.imgName = counter
counter++
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.