UICollectionViewCell(Swift)中的UICollectionView

Tur*_*ase 4 iphone ios uicollectionview uicollectionviewcell swift

我试图UICollectionView在每个可重用的内部放置一个UICollectionViewCell.该灰沟方法并没有为我工作了太清楚,因为使用一个UICollectionViewController类的两个数据源和委托UICollectionViews不起作用.

我最新的方法是UICollectionViewController在每个单元格中放置一个视图,如本问题本文档中所述.但是,当我尝试加载视图时,我的应用程序冻结了.在Xcode调试导航器中,CPU处于恒定的107%,几秒钟后内存接近1GB.

在这个例子中,我试图ThumbnailCollectionViewController在每个中获得一个MainCollectionViewControllerCell.每个中唯一的ThumbnailCollectionViewControllerCell是尺寸为50x50的图像.

这怎么做得好?

在MainCollectionViewController中

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell

    let thumbsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ThumbnailColllection") as! ThumbnailCollectionViewController
    self.addChildViewController(thumbsViewController)
    thumbsViewController.view.frame = cell.bounds
    cell.addSubview(thumbsViewController.view)
    thumbsViewController.didMoveToParentViewController(self)
} 
Run Code Online (Sandbox Code Playgroud)

ThumbnailCollectionViewController

let reuseIdentifier = "ThumbCell"
let thumbnails = ["red", "green", "blue"]

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return thumbnails.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
    let cellImage = UIImage(named: thumbnails[indexPath.row])
    let cellImageView = UIImageView(image: cellImage)
    cellImageView.frame = cell.bounds
    cell.addSubview(cellImageView)

    return cell
}
Run Code Online (Sandbox Code Playgroud)

Stu*_*art 10

UICollectionView每个单元格放入内部的方法看起来很好,但是您很可能会看到恶劣的性能,因为您错误地处理了单元格重用.您正在向单元格添加新的集合视图(实际上,每个缩略图单元格都有一个新的图像视图)每次请求新的可重用单元格时.如果连续滚动,则单元格将会已经添加了这些子视图,因此您最终会在每个单元格下面显示许多子视图.

相反,添加一种检查单元格是否已经具有子视图的方法,只有在没有时才添加它.可能最好的方法是使用自定义单元子类:

class ThumbnailCollectionCell: UICollectionViewCell {
    var thumbnailViewController: ThumbnailCollectionViewController?
}
Run Code Online (Sandbox Code Playgroud)

回到MainCollectionViewController:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ThumbnailCollectionCell
    if cell.thumbnailViewController == nil {
        let thumbsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ThumbnailColllection") as! ThumbnailCollectionViewController
        self.addChildViewController(thumbsViewController)
        thumbsViewController.view.frame = cell.bounds
        cell.addSubview(thumbsViewController.view)
        thumbsViewController.didMoveToParentViewController(self)
        cell.thumbnailViewController = thumbsViewController
    }
}
Run Code Online (Sandbox Code Playgroud)

同样,类似于防止多个UIImageViews添加到每个缩略图单元格的东西.