Is it possible to design NSCell subclasses in Interface Builder?

后端 未结 10 1266
终归单人心
终归单人心 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:];
    if ( cell == nil )
    {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed: 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.

提交回复
热议问题