2017-09-01 80 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()