Is it possible to design NSCell subclasses in Inte

2019-01-21 23:28发布

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 and then load the NSCell from a nib.

Is this possible? How do I do it?

10条回答
淡お忘
2楼-- · 2019-01-21 23:47

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

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.

查看更多
贼婆χ
3楼-- · 2019-01-21 23:49

Some answers in this thread have gone off topic because they're talking about Cocoa Touch, when the original question was about Cocoa - the 2 APIs are quite different in this regard and Cocoa Touch makes it easy because UITableViewCell is a view subclass. NSCell isn't, and that's the problem

For information, I had to do something very similar in NSOutlineView recently - which is basically the same, but a little harder if anything because you have to deal with disclosure / collapse of levels. If you're interested in the code, I posted about it here: http://www.stevestreeting.com/2010/08/08/cocoa-tip-using-custom-table-outline-cells-designed-in-ib/

HTH

查看更多
仙女界的扛把子
4楼-- · 2019-01-21 23:53

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.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-22 00:00

Add your UITableViewCell to your tableviewcontroller and declare an IBOutlet property:

@interface KuguTableViewController : UITableViewController {
    IBOutlet UITableViewCell *customTypeCell;
}

@property (readonly)  UITableViewCell *customTypeCell;

... then in cellForRowAtIndexPath you can just use your cell and set it to be reused:

static NSString *CellIdentifier = @"CustomCell"
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
        cell = customTypeCell;
        cell.reuseIdentifier = CellIdentifier;
查看更多
登录 后发表回答