简体   繁体   English

使用数组项属性值显示在UITableView / UICollectionView中

[英]Using an Array Items Property Value to display in UITableView/UICollectionView

I know how to use an array to display in a UItableView or UICollectionView but unsure how to specify the data. 我知道如何使用数组显示在UItableViewUICollectionView但不确定如何指定数据。

So instead of simply displaying all items in the array, to filter the results using a items property value. 因此,与其简单地显示数组中的所有项目,不如使用items属性值来过滤结果。 For for eg each item in the array has a property called number, if the number is 2 then display, products[indexPath.row].number == 2 if the .number == 1 then don't. 例如,对于数组中的每个项目,都有一个名为number的属性,如果显示的数字为2,那么如果.number == 1 ,则显示products[indexPath.row].number == 2

In my application I have a fetch request which I create an array to display in a collectionView. 在我的应用程序中,我有一个获取请求,我创建了一个数组以显示在collectionView中。 Each Product has a property called number , but how do I display products with a number equal to 2 ? 每个Product都有一个名为number的属性,但是如何显示数字等于2的产品

Fetch Request: 提取请求:

var products = [Product]()

func loadData() {

    var error: NSError?

    let moc1 = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let request1 = NSFetchRequest(entityName: "Product")
    request1.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
    self.products = moc1?.executeFetchRequest(request1, error: &error) as! [Product]

    self.collectionView.reloadData()
}

CollectionView: CollectionView:

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

    return products.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell

    cell.textLabel?.text = self.products[indexPath.row].title

    return cell

}

So, let's say you got your array of products: 因此,假设您有一系列产品:

let products =  [
                    [ "name" : "t-shirt", "number" : 3 ],
                    [ "name" : "shirt", "number" : 1 ],
                    [ "name" : "shorts", "number" : 2 ],
                    [ "name" : "top", "number" : 2 ]
                ]

Then you can simply filter the array using Swift's filter function like this: 然后,您可以使用Swift的filter函数简单地过滤数组,如下所示:

let filteredProducts = products.filter({ product in
    product["number"] == 2
})

Or even shorter: 甚至更短:

let filteredProducts = products.filter{$0["number"] == 2}

filteredProducts will then contain every product with number = 2 . 然后, filteredProducts将包含number = 2每个product You can then use that array for your UICollectionView or simply just override your existing products variable. 然后,您可以将该数组用于UICollectionView或者只是覆盖现有的products变量。

I don't know the details about your Products object, but you probably want to do something like: 我不知道有关您的Products对象的详细信息,但是您可能想要执行以下操作:

let filteredProducts = products.filter{$0.number == 2}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM