带有Checkbox Cell的NSTableView

Mr *_*eph 6 macos cocoa objective-c

NSTableViewXcode 4上添加到我的xib后,我将其设置为4列.第1列是一个简单的列,它将包含项目的名称.其他3个是复选框.我将a Check Box Cell从对象库拖到tableview.

我填充表格并创建并显示复选框,但是如果我点击没有任何反应,我无法检查或取消选中它们.此外,我甚至不知道如何通过代码来做到这一点.

我该如何工作:能够选中或取消选中复选框并从代码中获取其状态.

我已经看到了这个问题,并没有真正回答我的问题.

以下是根据要求处理表的一些代码:

- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
    return (int)[myArray count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
    if([[tableColumn identifier] isEqualToString:@"col1"])
    {
       return[NSNumber numberWithInt:NSOffState];
    }    

    return [myArray objectAtIndex:row];
}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSLog(@"%d", [anObject boolValue]);
    if([[tableColumn identifier] isEqualToString:@"col1"])
    {
        NSLog(@"click col1");
    }        
    if([[tableColumn identifier] isEqualToString:@"col2"])
    {
        NSLog(@"click col2");
    }        

}
Run Code Online (Sandbox Code Playgroud)

我刚添加了更多代码.如何设置它来检查/取消选中?

小智 8

该模型

您需要决定模型,即您将如何表示在表视图中显示的数据.例如:

// SomeObject.h
#import <Foundation/Foundation.h>
@interface SomeObject
@property (copy) NSString *name;
@property (assign,getter=isVisible) BOOL visible;
@property (assign,getter=isOpaque) BOOL opaque;
@property (assign,getter=isAnimatable) BOOL animatable;
@end

// SomeObject.m
#import "SomeObject.h"
@implementation SomeObject
@synthesize name, visible, opaque, animatable;
- (void)dealloc {
    [name release];
    [super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)

nib文件

为了这个答案,给表格列标识符匹配属性名称SomeObject.

提供从模型到表视图的值

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
    // Retrieve the model object corresponding to `row'
    SomeObject *obj = [myArray objectAtIndex:row];

    // Return the object property corresponding to the column
    if([[tableColumn identifier] isEqualToString:@"name"])
    {
        return obj.name;
    }
    // Since this method has return type `id', we need to box the
    // boolean values inside an `NSNumber' instance
    else if([[tableColumn identifier] isEqualToString:@"visible"])
    {
        return [NSNumber numberWithBool:obj.visible];
    }
    else if([[tableColumn identifier] isEqualToString:@"opaque"])
    {
        return [NSNumber numberWithBool:obj.opaque];
    }
    else if([[tableColumn identifier] isEqualToString:@"animatable"])
    {
        return [NSNumber numberWithBool:obj.animatable];
    }

    return nil;
}
Run Code Online (Sandbox Code Playgroud)

使用表视图中的值更新模型

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    // Retrieve the model object corresponding to `row'
    SomeObject *obj = [myArray objectAtIndex:row];

    // Set the object property corresponding to the column
    if([[tableColumn identifier] isEqualToString:@"name"])
    {
        obj.name = anObject;
    }
    // Since the new value (`anObject') is an object, we need to
    // convert it to `BOOL' by sending it `-boolValue'
    else if([[tableColumn identifier] isEqualToString:@"visible"])
    {
        obj.visible = [anObject boolValue];
    }        
    else if([[tableColumn identifier] isEqualToString:@"opaque"])
    {
        obj.opaque = [anObject boolValue];
    }        
    else if([[tableColumn identifier] isEqualToString:@"animatable"])
    {
        obj.animatable = [anObject boolValue];
    }
}
Run Code Online (Sandbox Code Playgroud)

通过使用键值编码可以使此代码更简单,但在掌握表格视图数据源之后,这仍然是一个练习.:P