GithubHelp home page GithubHelp logo

Comments (6)

ndizazzo avatar ndizazzo commented on July 20, 2024 1

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.

AliSoftware avatar AliSoftware commented on July 20, 2024

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.

AliSoftware avatar AliSoftware commented on July 20, 2024

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.

ndizazzo avatar ndizazzo commented on July 20, 2024

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.

screen shot 2018-12-28 at 2 56 32 pm

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:

  1. Load a file's owner XIB with connected outlets into a view outlet
  2. Using the custom extensions above, dynamically attach that @IBOutlet UIView (which is NibOwnerLoadable into the contentView property of the cell.

from reusable.

ndizazzo avatar ndizazzo commented on July 20, 2024

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.

AliSoftware avatar AliSoftware commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.