0

I have a table view presenting information about specific objects and I want the very first static cell to contain a photo gallery with a variable number of pictures.

Is it possible to just make a collectionview the subview of the static table view cell and implement all the collectionview methods or is there a better way to achieve this.

I did a research, but couldn't find any usable demonstrations..

PS: I do everything programmatically, without the use of storyboards or xibs and I am fairly beginner in programming, learning by doing, but I need your advice for my project.

This is how it should look like:

enter image description here

kkuilla
  • 2,226
  • 3
  • 34
  • 37
leyke077
  • 59
  • 7

2 Answers2

1

Yes you can use collection view as a subview of tableview cell to show photo gallery. You can implement its delegate datasource in same class.

KavyaKavita
  • 1,581
  • 9
  • 16
1
  1. In case you only want only one UICollectionViewCell, you could embed a UICollectionView to your first UITableViewCell and set the delegate and dataSource to your UITableViewController.

  2. In case you want to design it a bit more reusable, you could create a UICollectionViewController and embed it to your cell. <- I suggest you do that

Update:

According to the drawing, you might be interested in using the tableHeaderView for the gallery instead of a static cell. Both solutions from above (1 and 2) are applicable there, but using the TableHeaderView is much more convenient. You can set it via

self.tableView.tableHeaderView = yourGalleryCollectionView
MarkHim
  • 5,686
  • 5
  • 32
  • 64
  • I'll try it with tableHeaderView, then I need to set the collectionview delegate and datasource to self ? – leyke077 Oct 26 '15 at 11:23
  • Yes make your tableViewController the delegate and, and make sure it implements the collectionview delegate and dataSource protocol – MarkHim Oct 26 '15 at 11:24
  • I don't use a tableviewController, instead a uiviewcontroller with a tableview – leyke077 Oct 26 '15 at 11:47
  • I am having trouble setting the height of the tableheaderview and setting detailTableView.tableHeaderView?.backgroundcolor = UIColor.greenColor() doesn't do anything, plus my image won't be displayed, maybe the whole collectionview isn't displayed.. – leyke077 Oct 26 '15 at 12:04
  • Can't really help you without more info. Try starting simple to debug: create an empty uiview, set the frame (the height is relevant), and set its backgroundColor. Then do self.tableView.tableHeaderView = yourSimpleView – MarkHim Oct 26 '15 at 12:14
  • I now have the collectionView in the tableHeaderView with the right height and a backgroundcolor. I wrote print("cell for row at index path") into my collectionview method, but it doesn't get called. I have set the galleyCollectionView.delegate and .dataSource to self.. – leyke077 Oct 26 '15 at 12:20
  • Since it is definately possible to solve your problem with tableHeaderView, you should close this question and consider starting a new question with your new problem and showing some of your code. I will gladly look there as well. Comments are not really good for problem solving here – MarkHim Oct 26 '15 at 12:23
  • I forgot to change vc.detailsTableView.reloadData() to vc.galleryCollectionView.reloadData() ;) – leyke077 Oct 26 '15 at 12:31