Is it possible to design NSCell subclasses in Interface Builder?

后端 未结 10 1234
终归单人心
终归单人心 2020-12-13 15:09

I\'m trying to subclass NSCell for use in a NSTableView. The cell I want to create is fairly complicated so it would be very useful if I could design it in Interface Builder

相关标签:
10条回答
  • 2020-12-13 15:27

    In IB, start an empty XIB. Now go to the pallete and drag in a UITableViewCell, double click to bring up and edit.

    include only the custom UITableViewCell (no other UIViews or other top level controls) - make sure it's a real UITableViewCell in IB, or you cannot set a reuse identifier (as opposed to casting a UIView in IB as your custom UITableViewCell class). Then you can add lables or whatever you like within the cell, as well as setting the reuse identifier or set whatever disclosure indicator you might like.

    To use, you provide code like this in the tableView:cellForRow:atIndexPath: method:

    YourCustomCellClass *cell = (YourCustomCellClass *)[tableView dequeueReusableCellWithIdentifier:<IDYouSetInXIBFile>];
    if ( cell == nil )
    {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:<YourXIBName> owner:self options:nil];
        id firstObject = [topLevelObjects objectAtIndex:0];
        if ( [ firstObject isKindOfClass:[UITableViewCell class]] )
            cell = firstObject; 
        else cell = [topLevelObjects objectAtIndex:1];
    }
    

    If you have any labels or other controls you want to reference in your code, wire them in IB to your custom cell class - NOT the file's owner, which you do not ever need to set using the above code (you can leave it as NSObject).

    Edit: I note you are really looking for an NSCell answer, but the code approach to using IB should be identical in Cocoa with the Cocoa Touch code I used above as loadNibNamed is a standard Cocoa call.

    0 讨论(0)
  • 2020-12-13 15:27

    I found some interesting examples which I do not totally understand, though.

    • GitX extends the NSTextFieldCell in their PBIconAndTextCell, referencing this post.
    • WWDC 2009 - Session 110 "Presenting User Data with Table Views and Browsers" talks "Adding subviews" and "Custom cell editors". (I do not have the source code, though.)
    • Display an NSTextfieldCell containing text and an image within a NSTableView, #70
    • Display an NSTextfieldCell containing text and an image within a NSTableView, #71

    The last 2 examples work with NSTableViewDataSource and NSTableViewDelegate. I would like to use Bindings an ArrayController in the InterfaceBuilder to connect other UI elements like text fields.

    • I stumbled into another discussion where Abizern points out PXListView by Alex Rozanski which looks very promising!

    • I am trying to implement a solution for the problem myself. Please find my project on github and the latest rendering problems over here.

    0 讨论(0)
  • 2020-12-13 15:30

    As Ken says, NSCells and NSViews are different, and you can only lay out NSView hierarchies in NIB, not NSCells (which don't have any explicit hierarchy).

    On the other hand, there's nothing preventing you from having a hierarchy of NSViews and using that to draw your NSCell -- you could add them as a subview of your cell's parent view, tell them to display, and remove them from the window and nobody would be the wiser.

    In this case, using a NIB would work, although it seems like a ton of hassle. Typically I've just replaced the object that takes NSCells with a custom one that takes my NSViews, but that means writing your own mouse-handling code, which is very touchy.

    On the other hand, my approach lets you bind the views' values in NIB, so you don't have to do any extra work, which is cool.

    0 讨论(0)
  • 2020-12-13 15:34

    Joar Wingfors wrote an article for Stepwise a few years back on a related topic, Subviews in TableView Rows.

    The principal technique is to create an NSCell that can host an NSView. If you were to do this, you could then design an NSView subclass in Interface Builder that you could embed anywhere you need that specific cell.

    Another possibility, if you can target Leopard, is to see whether you need to use an NSTableView or whether you can use an NSCollectionView. Collection views deal directly in terms of "item views" rather than in cells, so they're much more straightforward to design in Interface Builder.

    0 讨论(0)
  • 2020-12-13 15:40

    I want to provide a more modern approach here.

    Starting with iOS 5, UITableView has a method

    (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

    Once you registered your NIB containing your cell, just use

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

    to get a new cell. If a cell is available for reuse, it will be returned, otherwise a new cell is automatically created, and in that case this means loaded from the NIB file.

    0 讨论(0)
  • 2020-12-13 15:42

    The question was about a subclass of NSCell; the other answers seem to be doing something else, likely taking advantage of UITableViewCell being a view.

    NSCell is not a view. While laying a custom cell out in IB would be a useful thing to be able to do, I think the answer is basically "no, this is not possible". When you subclass NSCell, you're pretty much just doing your own drawing. There isn't support subcells, or parameterized auto layout (ala NSView's springs and struts), which is I suspect what you're looking for.

    The only caveat is that you could design an NSCell subclass that did do layout of sub-elements and provided parameters for setting those subelements and all tweakable parameters. Then, you would need to write an IB plugin to make that cell and accompanying inspector available at design time in IB.

    This, however, is probably harder than writing a little custom app that does more or less the same thing. Put an NSCell in a control in the middle of a window, and make yourself UI for tweaking the parameters you're interested in. Bindings can make this pretty straightforward for positioning stuff (i.e. bind an x value to a slider), though you will not get direct manipulation of the elements of course. When you're done, you could archive your cell and load the archive at runtime in your real app, or you could just log out the properties and set them in code in your app.

    0 讨论(0)
提交回复
热议问题