Button inside a collection view

I have a question about how to make use of a button inside a collection view.

Background:

I have an app that keeps track of maintenance history which I show to the user in collection view cells tied to the date. I have added the ability to assign photographs and other documents to the maintenance history. I also wrote code so that a button is displayed in the collection view cell if a photograph exists for the user to click on to view the photograph.

Problem:

Since I can’t know in advance if a photograph will exist I can’t create view controllers in advance for the button point to, to view the photograph associated with each history (cell).

I tried creating a single tableview view controller that all the buttons point to and then added logic in the cellForItemAt function to create an object containing the photo images for each cell but that logic gets executed infinitely and I end up with an infinite number of the photos.

Question:

How do you structure the view controllers to allow the user to view additional documentation associated with a collection view cell?

Thanks in advance for any help!

I was able to fix my issue by using the didSelectRow function instead of a button in each cell and then placing my code that creates the object containing the photos in the didSelectRow function.