2016-09-19 44 views
0

我有一個UICollectionView,用戶可以選擇單元格來使用它的功能。所以我需要它將第一個單元格設置爲在第一個屏幕截圖中進行選擇。但是問題出現在單元格被選中並且用戶想要選擇第一個單元格不能取消選擇的其他單元格。我嘗試它來重新加載tableview中沒有選擇的項目,但不能運行或與此func在viewdidload LabelCollectionView.selectItem(at: IndexPath(row: 1, section: 0), animated: true, scrollPosition: .bottom) SelectItem在UICollectionView上第一次加載

有沒有人有一個想法,我可以在那裏做什麼?感謝您的幫助:)

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

什麼是你想與檢查indexPath.row == indexPath.row辦?基本上,您可以使用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

是的,它是如何工作的,你應該將它標記爲selected = true來更改UI,並在選擇其他單元格時將其設置爲false。使用上述代碼進行操作嗎? – Santosh

+0

是的,我已經嘗試過,但它不能運行給它也許在didSelectItemAt其他方式? –

0

你應該使用Cells選擇狀態,而不是比較兩個相同的值。

在你自定義的單元格中,你對單元格選擇做出反應!

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功能。

在功能cellForItem ...你可以檢查,如果被選中的單元格,如果不能選擇一個與indexPath.row == 0

用於檢查所選單元格使用

collectionView.indexPathForSelectedItems 

這是列表,所以你可以選擇多個單元格。我不確定,但我認爲你可以通過多個選項設置collectionView,只有一個選擇。

我希望這可以幫助你到目前爲止。

相關問題