简体   繁体   English

在集合视图中滚动后,单元格不显示-Swift 2.1

[英]Cell not display after scroll in collection view - swift 2.1

Main Controller 主控制器

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        
}

故事板

I think its the way how your using them, try to define an array with images: 我认为这是您如何使用它们的方式,尝试使用图像定义数组:

var arrayOfImages = ["1","2","3","4"]

And use it in cellForItemAtIndexPath : 并在cellForItemAtIndexPath使用它:

cell.imageView.image = UIImage(named: arrayOfImages[indexPath.row])

Be sure to connect DataSoruce and Delegate for collectionView from connection inspector into your view controller to have the data loaded, and be sure to place the right image names. 确保将DataSoruceDelegate for collectionView从连接检查器连接到视图控制器中,以加载数据,并确保放置正确的图像名称。

Use the following code . 使用以下代码。 the reason for the image not being displayed is the you have not mentioned the file extension with the image as jpg or png ... your counter value is "1" so app cannnot load the image name 1 without extension png or jpg Use this code : 无法显示图片的原因是您未提及图片扩展名为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM