简体   繁体   English

NSTableView中的NSButtonCell复选框始终获取NSOffState。 为什么?

[英]NSButtonCell checkbox inside NSTableView is always getting NSOffState. Why?

Hi guys I'm new to Cocoa programming and I am getting always NSOffState whether I'm checking or unchecking an NSButtonCell (Check Box Cell in the UI dragged to a cell in an NSTableView). 嗨,大家好,我是Cocoa编程的新手,无论我正在检查还是取消选中NSButtonCell(UI中的Check Box Cell都拖到NSTableView的单元格中),我总是得到NSOffState。 I have a @property IBOutlet NSButtonCell *mySelection, connected to the respective UI and the following code. 我有一个@property IBOutlet NSButtonCell * mySelection,已连接到相应的UI和以下代码。

- (void) tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
MediaAComparar *media = [mediasRecesEnStock objectAtIndex:row];
NSString *identifier = [tableColumn identifier];

if ([identifier isEqualToString:@"seleccion"])
{
    if ([mySelection state] == NSOnState)
    {
        [media setValue:object forKey:@"seleccion"];
        NSLog(@"on state");
    }

    if ([mySelection state] == NSOffState)
    {
       [media setValue:object forKey:@"seleccion"];
        NSLog(@"off state");

    }

}

}

I never get the NSOnState to execute, the only NSLog message I get is: off state. 我从不执行NSOnState,我得到的唯一NSLog消息是:off state。 Can anyone give me some help? 谁能给我些帮助吗? Thanks!! 谢谢!!

If you have one outlet ("mySelection") and multiple rows, which row did you think the outlet connects to? 如果您有一个插座(“ mySelection”)和多行,那么您认为插座连接到哪一行? (Answer: none of them. You probably hooked it up to the prototype cell, which is never displayed or used directly.) (答案:没有。您可能将其连接到原型单元,该单元从不显示或直接使用。)

But no matter, you don't need to check the state before you set it. 但是无论如何,您无需在设置状态之前检查其状态。 Assuming your other code is correct, you should be able to do something like: 假设其他代码正确,则应该能够执行以下操作:

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    MediaAComparar *medium = [mediasRecesEnStock objectAtIndex:row];

    if ([tableColumn.identifier isEqualToString:@"seleccion"])
        medium.seleccion = object.booleanValue;
}

Less code is better code. 更少的代码就是更好的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM