创建 NSTableCellView 的子类。(适当的 .h 和 .m 文件)使类响应 NSTextFieldDelegate 协议。实现 control:textShouldEndEditing: 方法。使这个子类成为标签控件的代表。
这是一些示例代码。
类别列表单元.h
@interface CategoryListCell : NSTableCellView
@end
类别列表单元.m
@interface CategoryListCell()<NSTextFieldDelegate>
@property (weak) IBOutlet NSTextField *categoryLabel;
@property (assign) BOOL editing;
@property (copy) NSString* category;
@end
@implementation CategoryListCell
- (BOOL)control:(NSControl*)control textShouldBeginEditing:(NSText *)fieldEditor {
self.editing = YES;
return YES;
}
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor; {
if (self.editing) {
self.editing = NO;
[self mergeFromSource:self.category toDestination:self.categoryLabel.stringValue];
}
return YES;
}
- (void)mergeFromSource:(NSString*)source toDestination:(NSString*) destination {
// your work here
}
@end