NSTableView与复选框单元格 [英] NSTableView with Checkbox Cell

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

问题描述

在Xcode 4上添加 NSTableView 到我的xib后,我将其设置为4列。第一列是一个包含项目名称的简单列。其他3个复选框。我将复选框单元格从对象库拖动到表格。

After adding a NSTableView to my xib on Xcode 4 I set it to have 4 columns. The 1st column is a simple column that will contain the name of an item. The other 3 are checkboxes. I dragged a Check Box Cell from the object library to the tableview.

我填充表格,创建和显示,但是如果我点击没有发生任何事情,我不能检查或取消选中它们。此外,我甚至不知道如何通过代码。

I populate the table and the checkboxes get created and shown, however if I click on the nothing happens, I can't check or uncheck them. Furthermore, I don't even know how to do it by code.

我如何使这项工作:能够检查或取消选中复选框,并获得他们的状态代码。

How can I make this work: be able to check or uncheck the checkboxes and get their states from code.

我已经看到这个问题,它没有真正回答我的问题。

I already saw this question and it didn't really answer my question.

这里是一些照顾表的代码,请求:

Here is some of the code to take care of table, as requested:

- (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");
    }        

}

我只是添加了更多的代码。

I just added more code. How do I set it to check/uncheck?

推荐答案

模型



您需要决定一个模型,即如何表示在表视图上显示的数据。例如:

The model

You need to decide upon a model, i.e., how you’re going to represent the data that’s being shown on the table view. For example:

// 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



nib文件



为了这个答案,给出与 SomeObject 中的属性名称匹配的表列标识符。

The nib file

For the sake of this answer, give the table columns identifiers that match the property names in 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;
}



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



Using values from the table view to update the model

- (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];
    }
}

- 值编码,但是在你掌握表视图数据源之后,它仍然是一个练习。 :P

It is possible to make this code simpler by using Key-Value Coding but that’s left as an exercise after you master table view data sources. :P

这篇关于NSTableView与复选框单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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