CollectionView问题 - Swift

时间:2017-09-01 01:57:19

标签: ios swift3 uicollectionview uicollectionviewcell

我使用collectionView制作音乐播放器。我想选择collectionView之类的单选按钮。如果选择了另一个单元格,则不应选择所选单元格。但是没有用。

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

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

这是我的代码。

       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()
}

2 个答案:

答案 0 :(得分:0)

我认为问题是你在numberOfItems方法

中重新加载collectionView

所以只需删除

 self.practiceCollection.reloadData()

答案 1 :(得分:0)

将buttonCounter更改为Bool数组,其数量等于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()