Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UITableViewCell & UICollectionViewCell identifiers #19

Open
AliSoftware opened this issue Sep 25, 2015 · 17 comments
Open

Support UITableViewCell & UICollectionViewCell identifiers #19

AliSoftware opened this issue Sep 25, 2015 · 17 comments

Comments

@AliSoftware
Copy link
Collaborator

Generate something like this:

extension UITableViewCell {
  enum Identifiers : String {
    case Home
    case Menu
    case Product
    ...

    var nib: UINib { return UINib(nibName: self.rawValue, bundle: nil) }
  }
}

extension UITableView {
  func registerCell(identifier: UITableViewCell.Identifier) {
    self.registerNib(identifier.nib, forCellReuseIdentifier: identifier.rawValue)
  }
  func dequeueReusableCellWithIdentifier(_ identifier: UITableViewCell.Identifier,
      forIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return self.dequeueReusableCellWithIdentifier(identifier.rawValue,
                          forIndexPath: indexPath)
  }
}

Usage:

tableView.registerCell(.Home)
tableView.registerCell(.Menu)

var cell = tableView.dequeueReusableCellWithIdentifier(.Home, forIndexPath: indexPath)
@AliSoftware
Copy link
Collaborator Author

Probably better wait for #24 before starting to work on this issue

@AliSoftware AliSoftware modified the milestone: 0.8.0 Oct 14, 2015
@yoiang
Copy link

yoiang commented Nov 2, 2015

Hey @AliSoftware ! In developing a tool with similar features to SwiftGen I built a library that makes extracting (eventually) everything, including UITableViewCell and UICollectionViewCell identifiers in Storyboards much easier. It may be overkill for your use but the library is StoryboardKit.

To see UITableView Identifier extraction check seguecode's usage here: ViewControllerClassInfo+seguecode.swift Line 35-66

@AliSoftware
Copy link
Collaborator Author

Hey @yoiang

Thanks for the input, will take a look later in the week!

@phatblat
Copy link
Member

phatblat commented Nov 4, 2015

+1 on this. I suggest adding UICollectionViewCell to the issue title (they are easy to forget about 😉).

@AliSoftware AliSoftware changed the title Support UITableViewCell identifiers Support UITableViewCell & UICollectionViewCell identifiers Nov 4, 2015
@jmacmullin
Copy link

+1

@AliSoftware
Copy link
Collaborator Author

Feel free to make a PR to make this be implemented and released faster, as I'm pretty busy those days and won't have much time implementing it that soon.

@jmacmullin
Copy link

No worries :-)
#66

@AliSoftware AliSoftware modified the milestone: 0.9.0 Mar 12, 2016
@krodak
Copy link

krodak commented Mar 17, 2016

My comment is not related to SwiftGen, but as its user I'll comment anyway.
For UITableViewCell and UICollectionViewCell it's quite good practice to use class names as identifiers.
You can do it with extenstion like this one:

extension UITableView {

    public func dequeueReusableCell<T:UITableViewCell>(type: T.Type) -> T {
        let tableCell       : T
        let cellIdentifier  = String(T)

        if let cell = self.dequeueReusableCellWithIdentifier(cellIdentifier) as? T {
            tableCell = cell
        } else if let _ = NSBundle(forClass: T.classForCoder()).pathForResource(cellIdentifier, ofType:"nib") {
            registerNib(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier)
            if let cell = NSBundle(forClass: T.classForCoder()).loadNibNamed(cellIdentifier, owner: nil, options: nil)[0] as? T {
                tableCell = cell
            } else {
                //if anyone had better suggestion for fallback, you're welcome to comment
                tableCell = T(style: .Default, reuseIdentifier: cellIdentifier)
            }
        } else {
            tableCell = T(style: .Default, reuseIdentifier: cellIdentifier)
        }
        return tableCell
    }
}

In code you use it like this:

let cell = tableView.dequeueReusableCell(CommentVideoCell)

This actually solves 3 problems:

  • XIB naming
  • Identifier naming
  • Do don't need to register your cells explicitly

So you can avoid magic string in both cases.
Of course, there are cases when you need more than 1 identifier for some cell, but truly, during few years of iOS development I haven't found too many cases like this.

As for UICollectionViewCell you can find similar code here:
https://gist.github.com/krodak/25680b1ec18e750f6ddd

@AliSoftware
Copy link
Collaborator Author

@krodac I guess you didn't read about my comment on #66 and my pod Reusable library then 😉

@krodak
Copy link

krodak commented Mar 19, 2016

@aisoftware - yes, I missed it ;-) But looking at it now it's not exactly the same approach, is it?

@AliSoftware AliSoftware modified the milestone: 2.0.0 Jun 1, 2016
@phatblat
Copy link
Member

phatblat commented Jun 5, 2016

Just had a great idea for a template once this is implemented. Since Reusable looks up the cell identifier based on the value of the reuseIdentifier property, a swiftgen template could generate an extension for every custom *Cell class found, simply supplying the reuseIdentifer that was found in the storyboard. I think this would eliminate the possibility of the two getting out of sync.

@AliSoftware
Copy link
Collaborator Author

@phatblat Sounds good to me! Don't hesitate to piggy-back on #66 to discuss that and make it happen the way you just described!

@phatblat
Copy link
Member

phatblat commented Jun 5, 2016

I'm on it! 🏃

@AliSoftware
Copy link
Collaborator Author

The new reference PR to implement that is now SwiftGen/SwiftGenKit#16

@netbe
Copy link

netbe commented Oct 23, 2018

@AliSoftware is that still wip ?

@AliSoftware
Copy link
Collaborator Author

Hi @netbe

The feature is still definitely in our roadmap, and we actually renamed the storyboard subcommand to ib in the latest 6.0 release, and rearranged the parsers in SwiftGenKit too… in order to better prepare for supporting that (as this won't be limited to storyboards anymore, and parsers are better arranged now to be more flexible for that)

The previous Pull Requests though won't probably completely fit anymore the new code organization, so we might need to rewrite those, but that should actually be easier now, hopefully.

I won't give you a deadline though, as I'm not sure how much free time we'll have between all contributors to work on this, so when this will be done, but any contribution in that direction is welcome

@netbe
Copy link

netbe commented Oct 26, 2018

@AliSoftware thanks for the update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants