ios - 如何快速过滤 UICollectionView 中的单个类别?

标签 ios swift uicollectionview filtering

我是 swift 的新手,我在尝试过滤 UICollectionView 中的某些类别时遇到了一些问题。

这是我的代码,用于获取所有类别的所有文章。

    func getArticlesforCategory(category: String) -> Int {
    var result : Int = 0
    for article in self.allArticles {
        if category == article.category {
            result += 1
        }
    }
    return result
    }

如何只过滤一个类别,例如“测试”?

我通过解析wordpress网站的xml得到所有的分类

最佳答案

您可以使用filter 函数来过滤您的数组:

func getArticlesforCategory(category: String) -> Int {
    let filteredArray = allArticles.filter( {$0.category == category }) // Here you have filtered array
    return filteredArray.count // If you want number of items pass count of filtered array
}

关于ios - 如何快速过滤 UICollectionView 中的单个类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50484288/

相关文章:

ios - 如何在使用 xcode 开发 ios 应用程序时读取文本文件

iphone - restkit 安装到 xcode - 总是这么复杂吗?

iOS 构建错误 : Certificate doesn't match profile: The default keychain doesn't have an identity matching

xcode - 将 Xcode 的测试类助手编辑器与 Swift 类一起使用

swift - 如何快速压缩和解压缩密码保护

ios - Collection View 中类似表格 View 的动画?

ios - Swift:NSURL session 不适用于 watchOS

ios - swift 3 ios : UILabel

objective-c - UICollectionView 使用 performBatchUpdates 执行更新

ios - 使用 Xib 时 UITableView 和 UICollectionView 的实现有什么区别?