Could I reuse UICollectionView
for multiple collectionViews in same VC or in different VCs or in UISplitViewController
? Don’t want to create UICollectionView
again and again.
Asked
Active
Viewed 51 times
1

Andreas
- 45
- 7
-
You can create a Xib file that has a collection view as how as you want and call the Xib whenever you want a the collection view inside your view. The xib file will conform the collection view protocols – Vasilis D. Mar 14 '18 at 08:44
-
don’t use storyboard nor xib. doing all in programmatically – Andreas Mar 14 '18 at 08:50
-
It's the same you will have to create programmatically a Swift file that implements UIView and Collection delegates and do it.. It's possible but logically there are a lot of ways to do what you want – Vasilis D. Mar 14 '18 at 08:53
-
how can I do? could you name one way to me? – Andreas Mar 14 '18 at 08:58
-
@ΒασίληςΔ. how does xib solve his question? there will have to be two objects anyway (so not really reusing it), only thing he is reusing is the code, which he can do programmatically too – Milan Nosáľ Mar 14 '18 at 09:12
-
You will reuse the xib it's time and all you will have to do is the configuration for the source, you will not have to write again the code for the way that the data will be shown.. and if you use Sigleton or you have another pattern you will not have to re-write the code for the data neither. So in your main view you will add the xib that you will have already added and you will configure that xib.. don't you agree?? @MilanNosáľ – Vasilis D. Mar 14 '18 at 09:17
-
@ΒασίληςΔ. as far as I know, everything that you can do in xib you can do programmatically. xib is just a visual language for user interface. when xib is loaded, it is a view object. that object cannot be reused at two places at the same time, just as the object created programmatically. If OP created a class that takes care of data sources, etc., he does not need to use it (and I personally would never recommend xib/storyboards, but ok, some people prefer it). Relating to reuse, I am not aware that xib would have any advantage over programmatic approach (and I guess that goes vice versa). – Milan Nosáľ Mar 14 '18 at 09:22
1 Answers
0
You can "reuse" it, but since it can appear in the view hierarchy only once (can have only one superview
), you cannot present it twice at the same time. But it should be possible to show it in one VC, and then when presenting another VC to pass and present it there. However, by presenting it in the second VC, it will get removed from the first one's view
, so you will have quite a lot of work with managing this passing and reusing. I don't think that it is worth it. So even that I am saying that you can do it, I would recommend not to do it.

Milan Nosáľ
- 19,169
- 4
- 55
- 90
-
How about in `SplitViewController`? and I’m creating collectionView programmatically. I edited my question. – Andreas Mar 14 '18 at 08:49
-
@Andreas if you expect it to be on the screen twice at the same time, that will not be possible. it is a view object. that single object can be presented at one place, but cannot be at two places at once (similarly as you as a person cannot be at two places in the same time) – Milan Nosáľ Mar 14 '18 at 08:50
-
I thought that class can't do such thing. I asked bcoz there is other way to do. no other way right? – Andreas Mar 14 '18 at 08:57
-
@Andreas I know that creating an object from a class is relatively heavyweight operation, but this seems like extreme case - you will get a lot of additional code managing proper reusing and that just so you don't have to create a couple of objects.. Moreover, as I said, you cannot show the same object at several places on the screen - you would have to create a copy to have it twice on the screen – Milan Nosáľ Mar 14 '18 at 09:00