2016-09-19 5 views
0

UICollectionView가 있으며 사용자가 셀에서 함수를 사용하도록 선택할 수 있습니다. 그래서 첫 번째 스크린 샷에서 선택한 것과 같이 첫 번째 셀을 설정해야합니다. 그러나 문제는 셀이 선택되고 사용자가 첫 번째 셀을 선택 취소하지 않는 다른 셀을 선택하려고 할 때입니다. 내가 선택한 테이블 항목을 재로드하려고했지만 viewdidload에서이 func를 실행하거나 실행하지 마십시오 LabelCollectionView.selectItem(at: IndexPath(row: 1, section: 0), animated: true, scrollPosition: .bottom) 첫 번째로드시 UICollectionView의 SelectItem로드

내가 무엇을 만들 수 있는지 알 수있는 누군가가 있습니까? 도움을 주셔서 감사합니다 :) 당신이 indexPath.row == indexPath.row에 대한 검사와 함께 할 려

First Cell Selected

Here you see the Problem in the screenshot

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryLabelCell", for: indexPath) as! StoryLabelCell 


    let labels = Labels[indexPath.row] 


    cell.backgroundColor = UIColor.lightGray 
    cell.layer.cornerRadius = 10 

    cell.Title.text = labels 



    if indexPath.row == 0 { 
     cell.backgroundColor = darkgrau 
     cell.layer.borderColor = black.cgColor 
     cell.layer.borderWidth = 2.0 
    } 


    return cell 

} 



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell 


    if indexPath.row == indexPath.row { 
     cell.backgroundColor = darkgrau 
     cell.layer.borderColor = black.cgColor 
     cell.layer.borderWidth = 2.0 
    } 

} 




func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell 


    if indexPath.row == indexPath.row { 

     cell.backgroundColor = lightGray 
     cell.layer.borderWidth = 0 
    } 

} 

답변

0

? 기본적으로 cell.selected = true을 선택하면 선택 가능하고 선택을 취소하면 false으로 설정됩니다. 아래 코드를 시도해보고 작동하는지 알려주십시오.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryLabelCell", for: indexPath) as! StoryLabelCell 

    let labels = Labels[indexPath.row] 
    cell.backgroundColor = UIColor.lightGray 
    cell.layer.cornerRadius = 10 
    cell.Title.text = labels 

    if indexPath.row == 0 { 
     cell.backgroundColor = darkgrau 
     cell.layer.borderColor = black.cgColor 
     cell.layer.borderWidth = 2.0 
     cell.selected = true 
    } 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell 

    cell.backgroundColor = darkgrau 
    cell.layer.borderColor = black.cgColor 
    cell.layer.borderWidth = 2.0 
    cell.selected = true 
} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell 

    cell.backgroundColor = lightGray 
    cell.layer.borderWidth = 0 
    cell.selected = false 
} 
+0

다른 셀을 {index path.row == index path.row} else {}에서 밝은 회색 (선택되지 않음)으로 설정했지만 사용하지 않으면 선택된 셀만 표시됩니다. 그래서 그것은 모두 실행하지만 문제는 내가 다른 셀을 선택 (첫 번째 셀이 선택됨)하고 멀리 가지 않고 두 개의 셀을 선택하려고 할 때입니다. 어쨌든 다른 셀을 탭하면 첫 번째 셀의 선택 (밝은 회색)을 변경해야합니다. –

+0

예 어떻게 작동하는지, UI를 변경하려면 selected = true로 표시하고 다른 셀을 선택하면 false로 다시 설정해야합니다. 위의 코드를 사용 했습니까? – Santosh

+0

예. 시도해 보았지만 실행하지 않았 으면 didSelectItemAt에 다른 방법이 있습니까? –

0

같은 두 값을 비교하는 대신 셀 선택 상태를 사용해야합니다.

사용자 정의 셀에서는 셀 선택에 반응합니다!

override func setSelected(_ selected: Bool, animated: Bool) { 
    // change your cell like you want 
    if selected { 
     // make me dark 
    } else { 
     // make me light 
    } 
    // not tested, but somehow call the super 
    super.setSelected(selected:selected, animated: animated) 
} 

당신은 didSelect 및 didDeselct func을 삭제할 수 있습니다. 셀이 선택되면, 선택된 셀을 사용

collectionView.indexPathForSelectedItems 

이것을 확인하기위한 indexPath.row == 0

와 하나를 선택하지 않으면 함수 cellForItem에서

... 당신은 확인할 수 목록이므로 하나 이상의 셀을 선택할 수 있습니다. 확실하지는 않지만 여러 선택 항목을 가진 collectionView를 하나의 선택으로 설정할 수 있다고 생각합니다.

지금까지 도움이 되었기를 바랍니다.

관련 문제