[英]NSTableView with Checkbox Cell
在Xcode 4上向我的xib添加NSTableView
,我將其設置為4列。 第1列是一個簡單的列,它將包含項目的名稱。 其他3個是復選框。 我將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");
}
}
我剛添加了更多代碼。 如何設置它來檢查/取消選中?
您需要決定模型,即您將如何表示在表視圖中顯示的數據。 例如:
// 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
為了這個答案,給表格列標識符匹配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;
}
- (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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.