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

时间:2015-11-16 07:11:18

标签: ios swift swift2

主要控制器

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        
}

Storyboard

2 个答案:

答案 0 :(得分:0)

我认为你使用它们的方式,尝试用图像定义一个数组:

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

并在cellForItemAtIndexPath

中使用它
cell.imageView.image = UIImage(named: arrayOfImages[indexPath.row])

请确保将连接检查器中的collectionView DataSoruceDelegate连接到视图控制器以加载数据,并确保放置正确的图像名称。

答案 1 :(得分:0)

使用以下代码。没有显示图像的原因是你没有提到图像为jpg或png的文件扩展名...你的计数器值是“1”所以应用程序无法加载图像名称1而没有扩展名png或jpg 使用此代码:

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

}