Comments (6)
That makes sense too! I think that since the latter approach you mentioned seems to work nicely, I'm going to stick with that.
Happy holidays, and thank you for taking some time to jam on ideas for this! π
from reusable.
I'm not sure I understand what you're trying to do. According to Apple documentation, XIBs for UICollectionViewCell
and UITableViewCell
should NOT have anything set as the File's Owner, and should only have one top level view, which is expected to be that said UICollectionViewCell
or UITableViewCell
. That's how Apple's UICollectionView
and UITableView
expect their XIB-based cells to be constructed, and UIKit won't be happy if you design it in any other way (like the one you're suggesting by setting the File's Owner to your Cell class and have a content view for example, UIKit will crash when trying to dequeue cells from XIB designed like that)
All that is UIKit. As for Reusable
, the protocol matching what UIKit expects for XIB-based cells is NibReusable
, as suggested by the README. The NibOwnerReusable
protocol, otoh, is useful for other things β like reusable views or widgets for example β but definitely not for cells as that's not what UIKit expects
from reusable.
Ok reading your description again maybe what you tried to do was loading a NibOwnerLoadable view, designed in it's own XIb, inside the contentView of your UICollectionViewCell?
If that's the case, why not create a generic GenericCell<T: NibLoadable>: UICollectionViewCell, Reusable
in your code, whose implementation would just call T.loadNibContent()
to instantiate the reusable content from its dedicated XIB, and add it as subview of self.contentView
with all the constraints?
from reusable.
Ok reading your description again maybe what you tried to do was loading a NibOwnerLoadable view, designed in it's own XIB, inside the contentView of your UICollectionViewCell?
Yeah, sorry... I didn't exactly describe that before jumping into the questions.
why not create a generic GenericCell<T: NibLoadable>: UICollectionViewCell
The idea is that I've got a few different types of cell templates (5 of them) with swappable content inside each element of the cell (which happen to be other UIView
that are NibOwnerLoadable
) designed in XIBs. I want to keep those template and reusable views designed in IB if possible, which is why I don't think the generic cell strategy works in my case.
I'm also snapshot testing each type of cell and want to instantiate them programatically without dequeuing... ie: let cell = MyCustomCell(frame: CGRect(x: 0, y:0, width: 375, height: 80))
, but also leveraging the automatic dequeuing and loading that UIKit provides when using them with a collection view in the app.
I realize that I'm trying to mix paradigms here and typically developers should choose one (programatic) or the other (XIBs), but I figured I'd give this a shot and see if it worked. So far so good, but am wondering there are any better approaches. I've always struggled to make this elegant and straightforward...
So what I have now works well and is designed like you suggested:
- Load a file's owner XIB with connected outlets into a view outlet
- Using the custom extensions above, dynamically attach that
@IBOutlet UIView
(which isNibOwnerLoadable
into thecontentView
property of the cell.
from reusable.
By the way - the project is open source, you might find it easier to check out what I'm doing with the NIBs and setup on this branch if I'm still not describing it correctly:
Block-Equity/stellar-ios-wallet#200
from reusable.
Ah right more clear now ;)
Won't be able to look at the project from here as I'm on holiday with family and didn't bring my laptop, and browsing a big codebase just from the phone will be quite tough for me to see the big picture π
The way you describe your needs makes more sense now, but not sure if it would have it's place in the Reusable lib itself per se? I think if you create a generic-purpose UIView extension to fill the content with a subview, you'd be able to use such an extension in many use cases β I don't see why limit this to only contentView of cells after all β and not just for this.
Something like this (typed from my phone so sorry for any syntax error π ):
extension UIView {
func setFillView(_ view: UIView) {
self.subviews.forEach { $0.removeFromSuperview() }
self.addSubview(view)
// And the code to add all the constraints
// To make view snap left+right+top+bottom to self
}
}
Then you'll have your content views, ready to snapshot-test, as standalone UIViews designed in their own XIB and NibOwnerLoadable, and when you want to incorporate them in a cell or swap the content of an existing cell to a different template, you'll just have to load that NibOwnerLoadable template view and set it as fill view:
cell.contentView.setFillView(MyTemplate2.loadFromNib())
Or of course you could instead specialize your extension for cells and make the implementation directly reference the cell's contentView⦠which end up being quite close to the solution you wrote initially after all, right?
from reusable.
Related Issues (20)
- getting error on using cellType(for indexPath:)
- Support for new API introduced on iOS 13
- MKMapView support
- Feature request - allow to use UITableViewCell with custom style
- Swift Package Manager: reusable deployment target warning HOT 3
- SPM xibs (Bundle.module) HOT 1
- Xcode 12.5 warnings HOT 2
- Swift tools version update too high, lower version build failed
- UITableViewHeaderFooterView - Warning with NibReusable
- Sure, go ahead!
- ## Summary
- Feature request - access to root xib view
- Support for reusable UITableViewHeaderFooterView subclasses?
- Privacy Manifest
- Swift 5 Support? HOT 2
- Undefined symbols when compiling for unit tests HOT 1
- Xcode 11 beta 4 compiler error HOT 1
- loadNibContent always loads nib named after base class HOT 14
- is there a way to override sceneStoryboard for StoryboardSceneBased HOT 2
- Swift Package Manager HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reusable.