2014-12-18 2 views
0

TrophyCollectionViewCell에서 사용자 인터페이스 요소를 수정하는 데 문제가 있습니다.didCelectItemAtIndexPath에서 UICollectionViewCell을 편집 할 수 없습니다.

cellForItemAtIndexPath 메서드를 사용하여 수정하면 정상적으로 작동하며 정상적으로 작동합니다. 그러나 didSelectItemAtIndexPath 메서드에서 수정하면 아무런 변화가 없습니다.

예를 들어, 다음은 didSelectItemAtIndexPath에 있습니다

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

trophyImageView 숨겨진하지 않습니다. 전혀 아무것도하지 못합니다.

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

이 내가 잘못된 이미지를 설정 collectionView의 데이터 모델을 편집하고 제외 욕망 같은 효과를 만듭니다 (이뿐만 아니라 didSelectItemAtIndexPath에) 그러나 작동합니까 기본 데이터 모델을 편집

지정된 trophyImageView에 대한 저작물

참고 : placeHolderArray 성공적으로 cellForIndexPath 방법에 UIImagecell.trophyImageView 의 설정하는 collectionView 이미지 파일 이름을 공급한다.

왜 세포의 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() 
    } 

} 

UICollectionViewdidSelectAtIndexPathtrophyImageViewcellForIndexPath에 있지만 수정할 수 있다는 것입니다.

+0

당신은 didSelectItemAtIndexPath에서 셀을 수정해서는 안 어쨌든 - 만약 당신이한다면, 당신이 변경 한 것은 셀이 재사용 될 때 다른 인덱스 경로에 나타날 것입니다. – rdelmar

+0

이것이 사실 인 이유는 무엇입니까? (왜 trophyImageView를 제대로 편집하지 않는 이유는 무엇입니까?). 셀을 선택하면 실제로 scrollView를 비활성화하므로 추가 셀을로드하지 않습니다. – CaptainCOOLGUY

답변

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 
    }) 
} 
+0

또는 개별 셀을 다시로드 할 수있었습니다. 이 경우에는 아무런 차이가 없습니다. 재로드가 발생하여 데이터 모델을 셀과 일치시킵니다 (선택한 셀에 대한 trophyImageView의 현재 이미지가 제거됨). – CaptainCOOLGUY

+0

@CaptainCOOLGUY, didSelectItemAtIndexPath처럼 보이지 않습니다. 외부에서 아무 것도 저장하지 않았습니다. 주기가 '셀 표시> 클릭> 변경> 셀 새로 고침> 애니메이션'인 것처럼 보입니다.당신이 그것을 밖으로 시도해볼 수 있습니까? –

+0

아, 이제 당신이 의미하는 바를 이해하고, 주석이 달라도 상관 없습니다. 하지만 네, 일반적으로 셀의 견해에 대한 변경 사항을 재설정했을 것입니다. 원래 게시물의 코드를 편집하여 trophyImageView를 수정하면 코드가 주석 처리되었음을 보여줍니다. – CaptainCOOLGUY

관련 문제