NSButtonCell NSSwitchButton不显示复选框 [英] NSButtonCell NSSwitchButton doesn't display checked box

查看:254
本文介绍了NSButtonCell NSSwitchButton不显示复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码中的 NSButtonCell 工作正常,但是点击时它不显示检查图标。任何想法为什么?

The NSButtonCell from the code bellow works fine, but it doesn't visualy display the check icon when clicked. Any ideas why?

  - (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item {

        if ([[tableColumn identifier] isEqualToString:@"select"]) {

            NSButtonCell *cell = [[NSButtonCell alloc]init];
            [cell setButtonType:NSSwitchButton];
            [cell setTarget:self];
            [cell setAction:@selector(checkboxChanged:)];
            [cell setTitle:@""];

            return cell;

        } else {
            NSCell *cell = [tableColumn dataCell];
            return cell;  
        }  
    }

以下是操作代码:

- (IBAction)checkboxChanged:(id)sender {

    NSButtonCell *aCell = [[sender tableColumnWithIdentifier:@"select"]
                      dataCellForRow:[sender selectedRow]];


    if ([aCell state] == NSOnState) {

        NSLog(@"ON");
        [aCell setState:NSOffState];


    } else {

         NSLog(@"OFF");
        [aCell setState:NSOnState];
    }

}


推荐答案

我不知道为什么你的第一个解决方案不工作 - 也许你没有你的代理挂?请注意,数据源和委托是两个不同的东西。

I'm not sure why your first solution didn't work—maybe you didn't have your delegate hooked up? Note that the data source and the delegate are two separate things.

您在答案中实现的方法是一个数据源,不是正确的地方。数据源,特别是 numberOfRowsInTableView: tableView:objectValueForTableColumn:row: >返回正在呈现的内容,而不是创建视图/单元格对象。

The method you implemented in your answer, which is a data source, is not the proper place to do that. The data source, and particularly numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:, should only return the content being presented, not create view/cell objects.

我建议在xib中创建和配置NSButtonCell,如果你有一个,您在哪里创建表视图及其列 - 这也是设置每个列的单元格的正确位置。

I recommend creating and configuring the NSButtonCell in your xib, if you have one, or in the same place where you create your table view and its columns—that's also the correct place to set each column's cell.

这篇关于NSButtonCell NSSwitchButton不显示复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆