Ios UICollectionView在1节中包含随机重用标识符

Ios UICollectionView在1节中包含随机重用标识符,ios,swift,random,uicollectionview,reuseidentifier,Ios,Swift,Random,Uicollectionview,Reuseidentifier,我有一个16个单元格的UICollectionView。其中1个单元格(对单元格的游戏)必须是随机的。我想在集合视图中的单元格上随机设置游戏 我通过使用不同的部分使它在某种程度上起作用,但这与视觉上融入16的目的背道而驰 我还创建了一个字符串数组,其中一个字符串是“GameOver”-然后我对数组进行了洗牌,以自定义此单元格的显示方式 这也不起作用,因为它没有让我在手机上控制游戏 我如何通过创建2个原型单元标识符来使用故事板,将游戏中的1个单元和15个普通单元在总共16个单元的集合视图上随机排列

我有一个16个单元格的UICollectionView。其中1个单元格(对单元格的游戏)必须是随机的。我想在集合视图中的单元格上随机设置游戏

我通过使用不同的部分使它在某种程度上起作用,但这与视觉上融入16的目的背道而驰

我还创建了一个字符串数组,其中一个字符串是“GameOver”-然后我对数组进行了洗牌,以自定义此单元格的显示方式

这也不起作用,因为它没有让我在手机上控制游戏


我如何通过创建2个原型单元标识符来使用故事板,将游戏中的1个单元和15个普通单元在总共16个单元的集合视图上随机排列?

这里有一个相当简单的方法:

在视图控制器中,定义一个属性以保存以后可以引用的任何单元重用标识符数组。您可以设置初始的非随机配置,如下所示:

var reuseIdentifiers = ["GameOver", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal"]
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let identifier = reuseIdentifiers[indexPath.item]
    return collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
}
定义以下方法,并在视图控制器初始化期间、collectionView显示在屏幕上之前以及每次重新加载collection view数据之前调用它:

func randomizeReuseIdentifiers() {
    var randomized = [String]()

    for _ in initialIdentifiers {
        let random = reuseIdentifiers.removeAtIndex(Int(arc4random_uniform(UInt32(reuseIdentifiers.count))))
        randomized.append(random)
    }

    reuseIdentifiers = randomized
}
然后在
collectionView(cellForItemAtIndexPath:NSIndexPath)
方法中,为当前indexPath查找匹配的reuseIdentifier,如下所示:

var reuseIdentifiers = ["GameOver", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal"]
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let identifier = reuseIdentifiers[indexPath.item]
    return collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
}

这种方法还有一个好处,就是允许您将来向初始的
reuseIdentifiers
数组中添加额外的标识符,以便在一个或多个随机位置中包含其他类型的单元格。

这里有一种相当简单的方法:

在视图控制器中,定义一个属性以保存以后可以引用的任何单元重用标识符数组。您可以设置初始的非随机配置,如下所示:

var reuseIdentifiers = ["GameOver", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal"]
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let identifier = reuseIdentifiers[indexPath.item]
    return collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
}
定义以下方法,并在视图控制器初始化期间、collectionView显示在屏幕上之前以及每次重新加载collection view数据之前调用它:

func randomizeReuseIdentifiers() {
    var randomized = [String]()

    for _ in initialIdentifiers {
        let random = reuseIdentifiers.removeAtIndex(Int(arc4random_uniform(UInt32(reuseIdentifiers.count))))
        randomized.append(random)
    }

    reuseIdentifiers = randomized
}
然后在
collectionView(cellForItemAtIndexPath:NSIndexPath)
方法中,为当前indexPath查找匹配的reuseIdentifier,如下所示:

var reuseIdentifiers = ["GameOver", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal"]
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let identifier = reuseIdentifiers[indexPath.item]
    return collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
}

这种方法还有一个好处,就是将来可以在初始的
reuseIdentifiers
数组中添加额外的标识符,以便在一个或多个随机位置中包含其他类型的单元格。

您可以显示当前拥有的代码吗?您可以显示当前拥有的代码吗?这很有效!我甚至没有想到在cellForItemAtIndexPath方法中使用可重用标识符数组。有点超出了我最初的问题范围。。。如果我想把GameOver和Normal放在它们自己的阵列(2个阵列)中,你的答案是否可行?这里的想法是,我希望“正常”的细胞在点击后被禁用,同时始终保持游戏在屏幕上。如果它们都在同一个数组中,我有可能禁用GameOver单元,这是我不想要的。再一次。。。超出范围,仅请求您的输入。@Joe使用两个数组是可能的,但要复杂得多。有更简单的方法可以使它与一个数组一起工作。例如,您可以向视图控制器添加一个
isGameOver
属性,然后在单元格出列后但返回之前添加以下行:
cell.userInteractionEnabled=isGameOver?标识符==“GameOver”:true
。这基本上是说“如果游戏结束了,只有当此索引的标识符等于“GameOver”时,才启用此单元格的用户交互。如果游戏未结束,则始终启用此单元格的用户交互。”实际上,我可能已经用2个数组解决了这一问题。我将GameOver和Normal分离到它们自己的数组中。然后在randomizeReuseIdentifiers中,我只是将initialIdentifiers设置为gameOverArray+ContininUarray。不过我很喜欢你们关于三元运算符和禁用交互的想法。这可以派上用场。。。谢谢你的帮助!!!成功了!我甚至没有想到在cellForItemAtIndexPath方法中使用可重用标识符数组。有点超出了我最初的问题范围。。。如果我想把GameOver和Normal放在它们自己的阵列(2个阵列)中,你的答案是否可行?这里的想法是,我希望“正常”的细胞在点击后被禁用,同时始终保持游戏在屏幕上。如果它们都在同一个数组中,我有可能禁用GameOver单元,这是我不想要的。再一次。。。超出范围,仅请求您的输入。@Joe使用两个数组是可能的,但要复杂得多。有更简单的方法可以使它与一个数组一起工作。例如,您可以向视图控制器添加一个
isGameOver
属性,然后在单元格出列后但返回之前添加以下行:
cell.userInteractionEnabled=isGameOver?标识符==“GameOver”:true
。这基本上是说“如果游戏结束了,只有当此索引的标识符等于“GameOver”时,才启用此单元格的用户交互。如果游戏未结束,则始终启用此单元格的用户交互。”实际上,我可能已经用2个数组解决了这一问题。我将GameOver和Normal分离到它们自己的数组中。然后在randomizeReuseIdentifiers中,我只是将initialIdentifiers设置为gameOverArray+ContininUarray。不过我很喜欢你们关于三元运算符和禁用交互的想法。这可以派上用场。。。谢谢你的帮助!!!