Mac tableview

使用习惯了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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值