jingo_man
jingo_man

Reputation: 529

Filter / Search function on Array of Structs, to display within tableView

I have created my own Struct object, that is populated from the Photos framework with each "moment" an image was contained in, and other metadata.

struct MomentImage {
    var momentNo : Int
    var index : Int
    var image : UIImage
}

I would like to populate a tableView, split with:

In tableView(cellForRowAt indexPath), I have printed out:

print("location: \(indexPath.section) and \(indexPath.row)")

But the Section value only ever prints "0". I expected my print statements to occur through all the sections in this table (with my test data, this is actually 13 sections)

How do I filter / retrieve this object at these 2 indices from my array of var moments = [MomentImage]...

Using array.filter { } seems the most obvious way. How do I pass in a 2nd property to check (i.e. momentNo AND index) within the same call?

let filteredassets = assets.filter { $0.momentNo == indexPath.section }

is my current code.

Note, the images need to be encodable, hence extracting from the PHImageManager into a distinct UIImage within a Struct that should itself be encodable.

Upvotes: 0

Views: 69

Answers (1)

jingo_man
jingo_man

Reputation: 529

As provided by @Vicaren, applying a && after the 1st parameter converts into to a 2-property IF AND statement

$0 is still available to select, simply choose another available property to test.

My own example results in:

let filteredassets = assets.filter { $0.momentNo == indexPath.section && $0.index == indexPath.row }

Upvotes: 1

Related Questions