2016-10-14 72 views
1

匹配不同阵列的声音文件的每个各部分使用夫特3SWIFT 3:的CollectionView

我已经设置了的CollectionView与7个部分 - 当用户敲击细胞(按钮)特定声音将从播放声音阵列。

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

例如

1)听起来soundArray1将起到当用户点击细胞(按钮)在的CollectionView

2)第1声音在soundArray2将起到当用户在第2挖掘细胞(按钮)的CollectionView

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

其中声音是声音文件阵列1。然而,这个声音文件阵列现在正在collectionView的所有部分中播放。我无法解决如何分割每个声音阵列以匹配collectionView中的每个部分。

+0

使用'indexPathForItem(在点:CGPoint) - > IndexPath?'(使用按钮的'centre'作为点),然后使用'indexPath.section'确定从哪个数组中选择(使用'indexPath.row ')。 – pbasdf

+0

似乎没有一个函数indexPathForItem?最接近的是didSelectItemAt indexPath –

+0

请参阅[这里](https://developer.apple.com/reference/uikit/uicollectionview/1618030-indexpathforitem)。 – pbasdf

回答

0

您可以使用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 
} 

关于“不明确提及成员”的错误,我本以为在你要么使用UICollectionViewController具有collectionView属性上面的代码,或在创造了一个collectionView属性UIViewController,以引用收集视图。要添加一个(或检查它),你应该有你的类定义中的

@IBOutlet var collectionView : UICollectionView! 

。然后在故事板中,您需要将相关场景中的收藏视图链接到视图控制器。要做到这一点,请按住Ctrl键从场景顶部的视图控制器图标(黄色圆圈)拖动到集合视图。出现提示时,从网点的列表中选择“的CollectionView”:

enter image description here

您可以检查网络连接是否已经建立了连接检查在右侧窗格中:

enter image description here

+0

func indexPathForItem(at point:CGPoint) - > IndexPath?{ 如果indexPath.section == 0 { 返回听起来[indexPath.row] }否则如果indexPath.section == 1 { } 返回零 } –

+0

这是我已经设置了功能到目前为止,但得到大量的错误 –

+0

对不起 - 很快就会更快,所以越详细越好 - 谢谢! –

相关问题