【问题标题】:Loop through an array of [Any]循环遍历 [Any] 数组
【发布时间】:2018-04-18 07:14:01
【问题描述】:

我有一个定义为Any 的数组,由用户对象组成,每60 个元素就会有一个“是”字符串。 数组如下所示:

[user1,...user60,"Yes",user61,user62....,user119,"Yes"]

我希望将单元格 1 到单元格 60 显示为我数组中的用户,当数组中的项目为“是”时,显示另一个单元格,它只是一个图像。然后collection view会根据数组继续增长。

【问题讨论】:

  • 你想在 CollectionView 中显示什么?
  • 如果你想显示单个用户,你可以用你的分隔符拆分它,并删除“是”......如果你想显示整个“user1,...user60”你可以只需将其拆分为“是”即可。
  • Swift 的强类型是有原因的。帮自己一个忙,重新考虑你的模型。 [任何]是万恶之源。
  • 所以你不是在寻找建议,而是在寻找代码。我们不是来做你的工作的。您的问题清楚地表明了与前一个类似的研究湖。你甚至没有标记对你有帮助的答案,非常不礼貌......
  • 如果以下答案之一解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道您的问题已得到您满意的解决,并为帮助您的人提供帮助。 See here 以获得完整的解释。

标签: ios arrays swift uicollectionview


【解决方案1】:

我会假设 user1...user119 的类型是 User,而不是你可以这样做:

let collection: [Any] = [user1,...user60,"Yes",user61,user62....,user119,"Yes"] 

for element in collection {
    if let user = element as? User {
        //do whatever you want with user class
    } else let yesString = element as? String, yesString == "Yes" {
        //you found "Yes" string
    } else {
       //unknown type
    }
}

当你想使用 collectionView 数据源中的数据时,你可以使用相同的逻辑,从数组中获取元素并执行相同的操作来检查元素是 User 还是 String,值为 "Yes ”。

由于从问题中不清楚您是否需要集合的“是”元素,您可以先从数组中删除“是”元素,然后您将拥有一个数组[User]

由于您的collectionView 将有两种类型的单元格:用户单元格和图像视图单元格,您必须先注册它们才能用于collectionView,例如:

self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "userCellIdent")
self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "yesCellIdent")

(如果您对每种类型都有自定义子类,大多数情况下您应该为您希望注册的每种细胞类型将UICollectionViewCell.self 替换为YourCollectionViewCellSubclassName.self

cellForItemAt 你这样做:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let element = collection[indexPath.item]

    if let user = element as? User {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "userCellIdent", for: indexPath)
        //configure your cell with user data
        return cell
    } else if let yesString = element as? String, yesString == "Yes" {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "yesCellIdent", for: indexPath)
        //configure your with the image view when element in collection is "Yes"
        return cell
    } else {
        //you have something other than user and "Yes" in the collection
        fatalError()
    }
}

【讨论】:

  • 他可以在这里使用while循环吗? @Ladislav
  • @SaeedRahmatolahi 这一切都取决于他想做什么,如果我想遍历整个数组,我通常不使用 while 循环,我使用 for in 。例如,如果我想对数组元素做一些事情,直到我碰到某个让我停下来的元素,我会使用 while :)
  • 感谢您的代码。我已经成功显示图像视图,但奇怪的是第 60 个用户也显示在图像单元格中,当列表变大时,所有单元格都会移动它们的位置。
  • 不太清楚你的意思...用屏幕截图等更新问题...
【解决方案2】:

您可以使用:

 let arrCollection: [Any] = [user1,...user60,"Yes",user61,user62....,user119,"Yes"]


    for data in arrCollection{

        if let strYes  == data as? String{
            print(strYes)
            print("yes found")

        }else{
            print("not Found")
        }
    }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2014-06-04
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2018-04-01
    • 2013-12-16
    • 2011-01-25
    相关资源
    最近更新 更多