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
방법에UIImage
cell.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()
}
}
내 UICollectionView
이 didSelectAtIndexPath
에 trophyImageView
cellForIndexPath
에 있지만 수정할 수 있다는 것입니다.
당신은 didSelectItemAtIndexPath에서 셀을 수정해서는 안 어쨌든 - 만약 당신이한다면, 당신이 변경 한 것은 셀이 재사용 될 때 다른 인덱스 경로에 나타날 것입니다. – rdelmar
이것이 사실 인 이유는 무엇입니까? (왜 trophyImageView를 제대로 편집하지 않는 이유는 무엇입니까?). 셀을 선택하면 실제로 scrollView를 비활성화하므로 추가 셀을로드하지 않습니다. – CaptainCOOLGUY