2017-03-25 5 views
0

각 셀에 각각 선택되거나 선택되지 않을 수있는 이미지와 버튼이있는 컬렉션보기를 만들려고합니다. 처음에는 모든 것이 잘 작동하는 것처럼 보였습니다. 그러나 일부 버튼을 누르고보기를 통해 이동하면 'selected'버튼이 'not selected'로 바뀌고 그 반대도 마찬가지입니다.스위프트 3 선택 버튼 스크롤 할 때 콜렉션 뷰에서 포지션을 변경합니다.

사람들이 이미지를 바꾸는 데 문제가있어서 이미지를 캐싱하려고 시도한 것으로 나타났습니다. 이미지를 완전히 제거하려고했지만 문제가 남아 있습니다. 내의 buttonPressed 함수가

여기
override func collectionView(_: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return imageArray.count 

} 


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell 

    //cell.photo.image = imageArray[indexPath.row] 

    cell.selectButton.tag = indexPath.row 
    cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside) 

    return cell 

} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let length = collectionView.frame.width/3 - 1 


    return CGSize(width: length, height: length) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    //sets spacing between images 
    return 1.0 
} 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    //sets spacing between images 
    return 1.0 
} 

입니다 :

func buttonPressed(sender: UIButton) { 

    if sender.isSelected == true{ 
     sender.setTitleColor(UIColor.blue, for:UIControlState.normal) 
     sender.isSelected = false 
    }else{ 
     sender.setTitleColor(UIColor.red, for:UIControlState.normal) 
     sender.isSelected = true 
    } 
} 

어떻게 이런 일이 해결 될 수있다 여기에

는 내 컬렉션보기 설정인가?

답변

1

당신이 필요로 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

에서 셀을 작성 중에 배열를 타고 버튼을 조건 각 시간을 확인하고 모든 선택된 인덱스를을 유지하는 것입니다.

var mArray = [Int]() 


    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell 

     //cell.photo.image = imageArray[indexPath.row] 

     cell.selectButton.tag = indexPath.row 
     cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside) 

//check if indexpath.row is selected 
    if mArray.contains(indexPath.row){ 
       cell.selectButton.setTitleColor(UIColor.red, for:UIControlState.normal) 
       cell.selectButton.isSelected = true 
    }else{ 
       cell.selectButton.setTitleColor(UIColor.blue, for:UIControlState.normal) 
       cell.selectButton.isSelected = false 
    } 


     return cell 

    } 


func buttonPressed(sender: UIButton) { 

    if sender.isSelected == true{ 
     mArray.removeAtIndex(sender.tag) 
     sender.setTitleColor(UIColor.blue, for:UIControlState.normal) 
     sender.isSelected = false 
    }else{ 
     mArray.append = sender.tag 
     sender.setTitleColor(UIColor.red, for:UIControlState.normal) 
     sender.isSelected = true 
    } 
} 
+0

안녕하세요. 방금 재검토 한 결과보다 철저한 테스트를 거친 후에도 효과가 없습니다. 잘못된 항목이 'myArray = myArray.filter {$ 0! = sender.tag}'에 'mArray.removeAtIndex (sender.tag)'를 스왑하여 수정 한 배열에서 제거되는 문제가있었습니다. 항목을 선택 취소하고 선택한 버튼 스위치 위치를 빠르게 스크롤하면 무엇이 원인인지 알 수 있습니까? – Tom