UICollectionViewCell在didSelectItemAtIndexPath中不可编辑

时间:2014-12-18 07:24:37

标签: ios objective-c swift uicollectionview

我在修改TrophyCollectionViewCell s。

中的用户界面元素时遇到问题

当我使用cellForItemAtIndexPath方法修改它们时,它很好并按预期工作。但是,当我在didSelectItemAtIndexPath方法中修改它们时,不会发生任何更改。

例如,以下内容位于didSelectItemAtIndexPath

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as TrophyCollectionViewCell
cell.trophyImageView.hidden = true
cell.reloadInputViews()

这不会隐藏trophyImageView。它根本没有做任何事情。

然而,编辑基础数据模型确实有效(这也在didSelectItemAtIndexPath中):

self.placeHolderArray[indexPath.row] = ""
self.collectionViewController.collectionView!.reloadData()

除了我正在编辑collectionView的数据模型以设置指定trophyImageView的错误图像资源外,这会产生我想要的效果。

  

注意:placeHolderArray成功提供了collectionView   图像文件名设置为UIImage   cell.trophyImageView方法中的cellForIndexPath

那么为什么我无法正确编辑单元格的trophyImageView

这是我的完整代码。

import UIKit

class CollectionHolderViewController: MainPageContentViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var collectionViewController = UICollectionViewController()
    var placeHolderArray = ["derplol_trophy", "medallion_of_courage", "derp_trophy", "herp_trophy", "jordy_trophy", "cotton_trophy", "weird_trophy", "rash_trophy"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionViewController = self.storyboard!.instantiateViewControllerWithIdentifier("TrophyRoom") as UICollectionViewController

        self.collectionViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)

        self.addChildViewController(collectionViewController)
        self.view.addSubview(collectionViewController.view)
        self.collectionViewController.didMoveToParentViewController(self)

        self.collectionViewController.collectionView!.dataSource = self
        self.collectionViewController.collectionView!.delegate = self

        //self.flowLayout!.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as TrophyCollectionViewCell
        cell.contentView.frame = cell.bounds
        cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight //to fix Apple bug.

        cell.trophyImageView.image = UIImage(named: placeHolderArray[indexPath.row]) //THIS WORKS.

        return cell
    }

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

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as TrophyCollectionViewCell

        self.collectionViewController.collectionView!.scrollEnabled = false // disable scrolling so view won't move
        let innerOffset = collectionView.contentOffset as CGPoint // offset of content view due to scrolling
        let attributes = self.collectionViewController.collectionView!.layoutAttributesForItemAtIndexPath(indexPath) as UICollectionViewLayoutAttributes!
        let cellRect = attributes.frame // frame of cell in contentView

        var imageView = UIImageView(frame: CGRectMake(cellRect.origin.x + innerOffset.x, cellRect.origin.y + innerOffset.y, cellRect.size.width, cellRect.size.height))
        imageView.image = UIImage(named: placeHolderArray[indexPath.row]) //change.
        imageView.contentMode = UIViewContentMode.ScaleAspectFit

        var trophyCase = UIImageView(frame: CGRectMake(cellRect.origin.x + innerOffset.x, cellRect.origin.y + innerOffset.y, cellRect.size.width, cellRect.size.height))
        trophyCase.image = UIImage(named: "trophy_case")
        trophyCase.contentMode = UIViewContentMode.ScaleAspectFit
        self.view.addSubview(trophyCase)

        self.view.addSubview(imageView)


        cell.trophyImageView.hidden = true //THIS DOES NOT WORK.
        cell.reloadInputViews()

        //self.placeHolderArray[indexPath.row] = ""

        //self.collectionViewController.collectionView!.reloadData()

        UIView.animateWithDuration(0.5, animations: {
            imageView.frame = self.view.frame
            trophyCase.frame = self.view.frame
            }, completion: {
                (value: Bool) in
        })
    }
}

我的单元格的类,它在界面构建器中正确设置。

import UIKit

class TrophyCollectionViewCell: UICollectionViewCell {

    @IBOutlet var trophyImageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

}

奇怪的是,我的UICollectionView可以修改trophyImageView中的cellForIndexPath,但不会修改didSelectAtIndexPath中的{{1}}。

1 个答案:

答案 0 :(得分:0)

为什么需要这一行self.collectionViewController.collectionView!.reloadData()

好像是在动画开始播放之前重新加载。

修改

我指的是..

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
  ....
  cell.trophyImageView.hidden = true //THIS DOES NOT WORK.
  //cell.reloadInputViews()
  self.placeHolderArray[indexPath.row] = ""

  self.collectionViewController.collectionView!.reloadData() <<< THIS LINE

  UIView.animateWithDuration(0.5, animations: {
        imageView.frame = self.view.frame
        trophyCase.frame = self.view.frame
        }, completion: {
            (value: Bool) in
    })
}