使用习惯了iOS的tableview,在使用mac os tableview的时候还是有很大差别的。iOS tableview是单列。mac os tableview可以创建好几列。
一个最基本的mac os tableview使用
- 创建新的工程习惯不去勾选storyboard选项
- 进到项目之后删除掉MainMenu.xib中的Window
- 创建一个继承自NSWindowController的一个window。
- 在AppDelegate中导入刚建的window,并且设置为主窗口
- 设置完AppDelegate就该去设置xib文件。创建xib好几次失败所以写下这篇博客,让给后来人有所帮助
操作xib文件
把一个tableview拖进window中,展开会发现在Tabel View下面默认创建了两个cell,需要给这两个cell重新命名 例如Name/Image
![]()
下面的按照此方法炮制。
这时候运行你会发现window头部会出现你刚才命名的header
- 接下来就是系统给的cell不能满足这个demo的需求选中删掉
- 掉
- 下面不能满足需要按照此方法删掉
- 重新拖进去控件
- 下面的拖进去一个label
- 此时运行并没有发现什么作用
- 需要给刚才新拖进的控件命一下名字
- 做到这一步基本差不多了,就是给tableview拖线,设置代理
- 这时候运行数据还出不来,因为没有添加代理
- 好几次脱线失败原因是没有找对对象
选中Tabl View 往file woner上面拖代理。这是比你会发现file woner会出现这几个条件
可以放心去控制器里面设置数据了
#import "MainWindowController.h"
@interface MainWindowController ()<NSTableViewDelegate, NSTableViewDataSource>
@property (strong) NSMutableArray *tableContents;
@property (weak) IBOutlet NSTableView *tabelView;
@end
@implementation MainWindowController
- (NSString *)windowNibName {
return @"MainWindow";
}
- (void)windowDidLoad {
[super windowDidLoad];
self.window.title = @"table";
NSArray *tableData = @[@"NSQuickLookTemplate",
@"NSBluetoothTemplate",
@"NSIChatTheaterTemplate",
@"NSSlideshowTemplate",
@"NSActionTemplate",
@"NSSmartBadgeTemplate",
@"NSIconViewTemplate",
@"NSListViewTemplate",
@"NSColumnViewTemplate",
@"NSFlowViewTemplate",
@"NSPathTemplate",
@"NSInvalidDataFreestandingTemplate",
@"NSLockLockedTemplate",
@"NSLockUnlockedTemplate",
@"NSGoRightTemplate",
@"NSGoLeftTemplate",
@"NSRightFacingTriangleTemplate",
@"NSLeftFacingTriangleTemplate",
@"NSAddTemplate",
@"NSRemoveTemplate",
@"NSRevealFreestandingTemplate",
@"NSFollowLinkFreestandingTemplate",
@"NSEnterFullScreenTemplate",
@"NSExitFullScreenTemplate",
@"NSStopProgressTemplate",
@"NSStopProgressFreestandingTemplate",
@"NSRefreshTemplate",
@"NSRefreshFreestandingTemplate",
@"NSBonjour",
@"NSComputer",
@"NSFolderBurnable",
@"NSFolderSmart",
@"NSFolder",
@"NSNetwork",
@"NSMobileMe",
@"NSMultipleDocuments",
@"NSUserAccounts",
@"NSPreferencesGeneral",
@"NSAdvanced",
@"NSInfo",
@"NSFontPanel",
@"NSColorPanel",
@"NSUser",
@"NSUserGroup",
@"NSEveryone",
@"NSUserGuest",
@"NSMenuOnStateTemplate",
@"NSMenuMixedStateTemplate",
@"NSApplicationIcon",
@"NSTrashEmpty",
@"NSTrashFull",
@"NSHomeTemplate",
@"NSBookmarksTemplate",
@"NSCaution",
@"NSStatusAvailable",
@"NSStatusPartiallyAvailable",
@"NSStatusUnavailable",
@"NSStatusNone"];
// Load up our sample data.
_tableContents = [NSMutableArray array];
// Our model consists of an array of dictionaries with Name/Image key pairs.
for (NSString *templateImageItem in tableData) {
NSImage *image = [NSImage imageNamed:templateImageItem];
NSDictionary *dictionary = @{@"Name": templateImageItem, @"Image": image};
[self.tableContents addObject:dictionary];
}
[self.tabelView reloadData];
}
// The only essential/required tableview dataSource method.
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.tableContents.count;
}
// This method is optional if you use bindings to provide the data.
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Group our "model" object, which is a dictionary.
NSDictionary *dictionary = self.tableContents[row];
// In IB the tableColumn has the identifier set to the same string as the keys in our dictionary.
NSString *identifier = tableColumn.identifier;
if ([identifier isEqualToString:@"MainCell"]) {
// We pass us as the owner so we can setup target/actions into this main controller object.
NSTableCellView *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
// Then setup properties on the cellView based on the column.
cellView.textField.stringValue = dictionary[@"Name"];
cellView.imageView.objectValue = dictionary[@"Image"];
return cellView;
} else if ([identifier isEqualToString:@"SizeCell"]) {
NSTextField *textField = [tableView makeViewWithIdentifier:identifier owner:self];
NSImage *image = dictionary[@"Image"];
NSSize size = image ? image.size : NSZeroSize;
NSString *sizeString = [NSString stringWithFormat:@"%.0fx%.0f", size.width, size.height];
textField.objectValue = sizeString;
return textField;
} else {
NSAssert1(NO, @"Unhandled table column identifier %@", identifier);
}
return nil;
}
@end
注意:我多次使用在创建windowController的xib文件没有成功,于是删掉xib。重新建xib。
http://www.jianshu.com/p/e9119da446d5
本文详细介绍了如何在 MacOS 应用程序中实现自定义的 TableView,包括创建窗口、配置 TableView 列、设置代理以及填充数据等步骤。

467

被折叠的 条评论
为什么被折叠?



