2016-09-19 4 views
0

12 셀이 포함 된 UICollectionView을 만들었습니다. 나는 그들의 색깔을 수돗물로 (같은 색깔로) 바꾸고 싶다. 여기 여러 UICollectionViewCell 색상이 함께 변경됨 문제

내 코드입니다 : 다시
  • 을 탭하면 내가의 [0] 세포의 [5를 탭하면

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
        let cell = collectionView.cellForItemAtIndexPath(indexPath)as! InterestCollectionViewCell 
        print("2 \(cell.interest) \(indexPath.row)") 
    
        if cell.selected == true { 
         cell.backgroundColor = UIColor.redColor() 
        } 
        else { 
         cell.backgroundColor = UIColor.clearColor() 
        } 
    } 
    

    문제

    1. 색상 다시 변경되지 않습니다 ] 및 [10] 셀도 색상이 바뀝니다. 나는 [1] 셀을 누르 후 동일, [6] [11] 세포가 대신 didSelectItemAtIndexPath의 색상을 설정하는 etc.m ... 너무
  • 답변

    1

    을 호출되는가에 대한 cellForItemAtIndexPath의 색상을 설정을 수행해야 Int의 인스턴스를 선언하고이 인스턴스에 collectionView의 행을 이와 같이 저장합니다.

    var selectedRow: Int = -1 
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell { 
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as! InterestCollectionViewCell 
        // Set others detail of cell 
        if self.selectedRow == indexPath.item { 
         cell.backgroundColor = UIColor.redColor() 
        } 
        else { 
         cell.backgroundColor = UIColor.clearColor() 
        } 
        return cell 
    } 
    

    지금 didSelectItemAtIndexPathselectedRowcollectionView를 다시 설정합니다.

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
        if self.selectedRow == indexPath.item { 
         self.selectedRow = -1 
        } 
        else { 
         self.selectedRow = indexPath.item 
        } 
        self.collectionView.reloadData() 
    } 
    

    편집 : 다중 셀 선택은 indexPath의 하나 개의 어레이를 만들고 같이 indexPath의 객체를 저장한다.

    var selectedIndexPaths = [NSIndexPath]() 
    
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell { 
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as! InterestCollectionViewCell 
        // Set others detail of cell 
        if self.selectedIndexPaths.contains(indexPath) { 
         cell.backgroundColor = UIColor.redColor() 
        } 
        else { 
         cell.backgroundColor = UIColor.clearColor() 
        } 
        return cell 
    } 
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
        if self.selectedIndexPaths.contains(indexPath) { 
         let index = self.selectedIndexPaths.indexOf(indexPath) 
         self.selectedIndexPaths.removeAtIndex(index) 
        } 
        else { 
         self.selectedIndexPaths.append(indexPath) 
        } 
        self.collectionView.reloadData() 
    } 
    
    +0

    안녕하세요 Nirav, 답변 해 주셔서 감사합니다. 둘 이상의 셀이 호출되고 색상이 다시 바뀔 수있는 문제를 수정합니다. 그러나 사용자가 하나 이상의 항목을 탭할 수 있도록 다중 선택을 허용하는 접근법을 알고 있습니까? – WoShiNiBaBa

    +0

    @WoShiNiBaBa 다중 선택에 대한 편집 된 답변 확인 :) –

    관련 문제