2016-09-19 71 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,只有一个选择。

我希望这可以帮助你到目前为止。