2017-09-01 53 views
0

我使用collectionView制作音乐播放器。我想在选择collectionView时喜欢单选按钮。如果选择另一个单元格,则不应选择所选单元格。但没有工作。CollectionView问题 - Swift

I dont want multi selection.https://i.stack.imgur.com/ATj3J.png

如何工作的? TT我需要你的帮助。谢谢。

这是我的代码。

 import UIKit 

class PracticeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 


var homeClassInPrac = HomeViewController() 
var songPlayOff = UIImage(named: "play") 
var songPlayOn = UIImage(named: "iconStop") 
var buttonCounter = [Int]() 
var freqNameList = ["Delta 1","Theta 1","Alpha 1","Beta 1","Delta 2","Theta 2","Alpha 2","Beta 2","Delta 3","Theta 3","Alpha 3","Beta 3","Delta 4","Theta 4","Alpha 4","Beta 4"] 


@IBOutlet var practiceCollection: UICollectionView! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    practiceCollection.delegate = self 
    practiceCollection.dataSource = self 
    self.practiceCollection.allowsMultipleSelection = false 

} 

func numberOfSections(in collectionView: UICollectionView) -> Int { 

    return 1 

} 

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

    self.practiceCollection.reloadData() 
    return freqNameList.count 

} 

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

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

    cell.pracImgView.image = songPlayOff 
    cell.pracLabel.text = freqNameList[indexPath.row] 
    if buttonCounter.contains(indexPath.row){ 
     cell.pracImgView.image = songPlayOn 
    } 
    else{ 
     cell.pracImgView.image = songPlayOff 
    } 
    return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 


     if buttonCounter.contains(indexPath.row){ 
      let index = buttonCounter.index(of: indexPath.row) 
      buttonCounter.remove(at: index!) 
      self.practiceCollection.reloadData() 
      homeClassInPrac.audioPlayer2.stop() 
     } 
     else{ 
      buttonCounter.removeAll() 
      buttonCounter.append(indexPath.row) 
      self.practiceCollection.reloadData() 
      let buttonInt = buttonCounter[0] 
      homeClassInPrac.playSound2(freqName: freqNameList[buttonInt]) 
    } 
print("Select\(buttonCounter)") 
} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
     print("Deselect\(buttonCounter)") 
     let index = buttonCounter.index(of: indexPath.row) 
     buttonCounter.remove(at: index!) 
     self.practiceCollection.reloadData() 
     print("Deselect\(buttonCounter)") 

} 





override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 
+0

你试过设置self.collectionView.allowsMultipleSelection =假在你的代码? – 3stud1ant3

+0

是的。已经有。 self.collectionView.allowsMultipleSelection = false在viewDidLoad。 –

+0

在你的代码覆盖func collectionView中添加这个函数(_reviewView:UICollectionView,didDeselectItemAt indexPath:IndexPath){ print(“Deselect”) } 并检查当你选择另一个项目时它是否被调用? – 3stud1ant3

回答

0

我觉得问题是你在numberOfItems方法重载的CollectionView

所以只是删除

self.practiceCollection.reloadData() 
0

变化buttonCounter至布尔数组等于计数freqNameList

var buttonCounter : Array<Bool> = [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false] 

在cellForItem方法中使用:

if buttonCounter[indexPath.row]{ 
     cell.pracImgView.image = songPlayOn 
    } 
    else{ 
     cell.pracImgView.image = songPlayOff 
    } 

在didSelectItem使用:

buttonCounter[indexPath.row] = true 
homeClassInPrac.playSound2(freqName: freqNameList[buttonInt]) 
self.practiceCollection.reloadData() 

在didDeselectItem使用:

buttonCounter[indexPath.row] = false 
self.practiceCollection.reloadData()