2017-11-20 2 views
0

사용자 지정 UICollectionViewFlowLayout 클래스를 사용하여 스크롤 뷰의 다중 스크롤 동작을 구현하고 있습니다. 그게 문제가 아니야. 그러나 사용자 지정 선택 및 선택 취소, 적절한 선택/선택 취소 shouldSelectItemAt 함수 안에 사용자 지정 코드를 사용해야합니다.Swift CollectionView 선택 취소가 작동하지 않습니다.

내부 MyCustomCollectionViewController :

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
if(collectionView == customContentCollectionView){ 

let cell:MyContentCell = collectionView.cellForItem(at: indexPath)! as! MyCollectionViewController.MyContentCell 
if(self.bubbleArray[indexPath.section][indexPath.item].umid != ""){ 

// DESELECTION LOGIC 
if(previouslySelectedIndex != nil){ 

let (bubbleBorder, bubbleFill) = getBubbleColor(selected: false) 

// following line is error prone (executes but may or may not fetch the cell, sometimes deselect sometimes doesn't) 
let prevCell = try collectionView.cellForItem(at: previouslySelectedIndex) as? MyCollectionViewController.MyContentCell 

prevCell?.shapeLayer.strokeColor = bubbleBorder.cgColor 
prevCell?.shapeLayer.fillColor = bubbleFill.cgColor 
prevCell?.shapeLayer.shadowOpacity = 0.0 
prevCell?.labelCount.textColor = bubbleBorder 
} 
previouslySelectedIndex = [] 
previouslySelectedIndex = indexPath 


// SELECTION LOGIC 
if(self.bubbleArray[indexPath.section][indexPath.item].interactions != ""){ 
let (bubbleBorder, bubbleFill) = getBubbleColor(selected: true) 
cell.shapeLayer.strokeColor = bubbleBorder.cgColor 
cell.shapeLayer.fillColor = bubbleFill.cgColor 
cell.shapeLayer.shadowOffset = CGSize(width: 0, height: 2) 
cell.shapeLayer.shadowOpacity = 8 
cell.shapeLayer.shadowRadius = 2.0 
cell.labelCount.textColor = UIColor.white 
} 
} 
return false 
} 
return true 
} 

MyContentCell의 코드 : 셀 선택 및 단지 밖으로 끌면

class MyContentCell: UICollectionViewCell{ // CODE TO CREATE CUSTOM CELLS 

var gradient = CAGradientLayer() 
var shapeLayer = CAShapeLayer() 
var horizontalLine = UIView() 
var verticalLeftLine = UIView() 

required init(coder aDecoder: NSCoder) { 
super.init(coder: aDecoder)! 
setup() 
} 

override init(frame: CGRect) { 
super.init(frame: frame) 
setup() 
} 

let labelCount: UILabel = { 
let myLabel = UILabel() 
return myLabel 
}() 


func setup(){ // initializing cell components here as well as later in cellForItemAt method 
............ 
} 

override func prepareForReuse() { 
self.shapeLayer.fillColor = UIColor.white.cgColor 
self.shapeLayer.shadowOpacity = 0.0 
} 
} 

이 문제가 될 수 있습니다 여기에 는 코드입니다 화면, 아마 파괴되지 않을 수도 있습니다, 그 이유는 재사용 기능에 대한 준비가 그것을 deselection을 추적하지 않습니다. 친절하게도 화면 밖에있는 셀 (가시적 인 인덱스)의 선택을 해제하는 방법을 알려주십시오.

+0

CollectionView를 무효화해야합니까? .collectionViewLayout.invalidateLayout()를 –

답변

0

당신은 레이아웃을

collectionView?.collectionViewLayout.invalidateLayout() 
+0

이 방법을 선택해야합니다 내부 작동하지 않았다. 이 코드는 어디에 두어야합니까? – dang

관련 문제