【问题标题】:SWIFT 3: Match different array of sound files to each each section in CollectionViewSWIFT 3:将不同的声音文件数组匹配到 CollectionView 中的每个部分
【发布时间】:2016-10-14 16:38:07
【问题描述】:

使用 Swift 3

我设置了一个包含 7 个部分的 collectionView - 当用户点击一个单元格(按钮)时,将从一组声音中播放特定的声音。

那么我如何拆分 collectionView 中的部分以匹配特定的声音数组?

例如

1) soundArray1 中的声音将在用户点击 collectionView 部分 1 中的单元格(按钮)时播放

2) soundArray2 中的声音将在用户点击 collectionView 部分 2 中的单元格(按钮)时播放

3) soundArray3 中的声音将在用户点击collectionView 第3 部分中的单元格(按钮)时播放

一直到第 7 节。

这是用户点击 cellButton 时的当前代码

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return sections.count
}

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

    if section == 0 {
        return self.appleProducts.count

    } else if section == 1 {
        return self.animals.count

    } else if section == 2 {
        return self.food.count

    } else if section == 3 {
        return self.activity.count

    } else if section == 4 {
        return self.travel.count

    } else if section == 5 {
        return self.objects.count

    } else if section == 6 {
        return self.symbols.count

    } else {
        return 0
    }
}

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

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

    if indexPath.section == 0 {
        cell.cellButton.setTitle(appleProducts[indexPath.row], for: UIControlState.normal)

    } else if indexPath.section == 1 {
        cell.cellButton.setTitle(animals[indexPath.row], for: UIControlState.normal)

    } else if indexPath.section == 2 {
        cell.cellButton.setTitle(food[indexPath.row], for: UIControlState.normal)

    } else if indexPath.section == 3 {
        cell.cellButton.setTitle(activity[indexPath.row], for: UIControlState.normal)

    } else if indexPath.section == 4 {
        cell.cellButton.setTitle(travel[indexPath.row], for: UIControlState.normal)

    }  else if indexPath.section == 5 {
        cell.cellButton.setTitle(objects[indexPath.row], for: UIControlState.normal)

    } else if indexPath.section == 6 {
        cell.cellButton.setTitle(symbols[indexPath.row], for: UIControlState.normal)
    }

    cell.cellButton.tag = indexPath.row

    return cell
}

@IBAction func cellButton(_ sender: AnyObject) {
    let sound =  (sender as! UIButton).tag
    self.setupAudioPlayer(file: Sounds[sound] as NSString, type: ".m4a")
    self.soundPlayer.play()
}

其中 Sounds 是声音文件的数组 1。然而,这个声音文件数组现在正在 collectionView 的所有部分播放。我不知道如何拆分每个声音数组以匹配 collectionView 中的每个部分

【问题讨论】:

  • 使用indexPathForItem(at point: CGPoint) -> IndexPath?(使用按钮的centre作为点)然后使用indexPath.section来确定从哪个数组中选择(使用indexPath.row)。
  • 好像没有函数indexPathForItem?最接近的是 didSelectItemAt indexPath
  • here
  • 谢谢您-您能否提供更多详细信息-例如 indexpathforitem 是独立功能还是在我的单元格按钮操作代码中?我的意思是在哪里放置这些功能的最佳位置?

标签: ios arrays swift audio


【解决方案1】:

您可以使用collectionView 方法indexPathForItem(at point: CGPoint) 来获取正确的索引路径。像这样的:

func didPressBtn(sender: UIButton) {
    let point = sender.center
    let cvPoint = self.collectionView!.convert(point, from: sender.superview)
    let ip = self.collectionView!.indexPathForItem(at: cvPoint)!        
    print("Button \(ip.section):\(ip.row) pressed")
    // You can then use ip.section to decide which array to use
    // and ip.row to choose the correct element from the array.  Like this:
    if (ip.section == 0) {
        self.setupAudioPlayer(file: soundsArray1[ip.row] as NSString, type: ".m4a")
        self.soundPlayer.play()}
    } else if (ip.section == 1) {
        self.setupAudioPlayer(file: soundsArray2[ip.row] as NSString, type: ".m4a")
        self.soundPlayer.play()}
    } else if (ip.section == 2) {
        self.setupAudioPlayer(file: soundsArray3[ip.row] as NSString, type: ".m4a")
        self.soundPlayer.play()}
    } // etc etc 
}

关于“对成员的歧义引用”错误,我在上面的代码中假设您正在使用具有 collectionView 属性的 UICollectionViewController,或者在 @987654331 中创建了 collectionView 属性@,引用集合视图。要添加一个(或检查它),你应该有

@IBOutlet var collectionView : UICollectionView!

在你的类定义中。然后在情节提要中,您需要将相关场景中的集合视图链接到您的视图控制器。为此,从场景顶部的视图控制器图标(黄色圆圈)按住 ctrl 并拖动到集合视图。出现提示时,从网点列表中选择“collectionView”:

您可以在右侧窗格的 Connections Inspector 中检查链接是否已建立:

【讨论】:

  • func indexPathForItem(at point: CGPoint) -> IndexPath?{ if indexPath.section == 0{ return Sounds[indexPath.row] } else if indexPath.section == 1 { } return nil }
  • 这是我目前设置的功能,但出现很多错误
  • 抱歉 - 对 swift 非常陌生,所以越详细越好 - 谢谢!!
  • @GuydeBromhead 无需实现该功能(indexPathForItem(at:)):collectionView 已经具有该方法。就像我在上面的代码中那样使用它。我将扩展它以准确展示如何获得正确的声音播放。
  • 非常感谢 - 我收到了基于此代码的最后一个错误 - 在这一行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多