2012-05-02 50 views
2

我只是可可開發人員用於Snow Leopard的初學者,我在編輯NSTableView中顯示的數據數組中的值時遇到問題。NSTableView數據源編輯

我嘗試在-tableView:setObjectValue:forTableColumn:row:之後編輯我的對象的某些屬性,之後我有EXC_BAD_ACCESS

我的NSTableView包含一列與NSButtonCell單元格,並且此列具有標識符'checked'。

我的代碼很簡單,看起來像這樣:

@interface MyObj: NSObject { 
    BOOL checked; 
} 
@property (assign) BOOL checked; 
@end 
@imlpementation MyObj 
@synthesize checked; 
@end 
@interface MyAppDelegate: NSObject <NSApplicationDelegate, NSTableViewDelegate, NSTableViewDataSource> { 
    NSMutableArray *data; 
    NSTableView *table; 
} 

@property (assign) IBOutlet NSTableView *table; 
- (NSInteger) numberOfRowsInTableView:(NSTableView *)aTableView; 
- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex; 
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex; 

@end 
@implementation MyAppDelegate 

@synthesize table; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    data = [[NSMutableArray array] retain]; 

     // some code that add few objects to data 
     // each object is instance of MyObj and I call alloc+init+retain for each 
     // ... 

    [table reloadData]; 
} 

- (void)dealloc { 
    [data release]; 
    [super dealloc]; 
} 

- (NSInteger) numberOfRowsInTableView:(NSTableView *)aTableView { 
    return (data == nil ? 0 : [data count]); 
} 

- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { 
    MyObj *obj; 
    obj = [data objectAtIndex:rowIndex]; 
    NSString *identifier; 
    identifier = [aTableColumn identifier]; 
    return [obj performSelector:NSSelectorFromString(identifier)]; 
} 

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { 
    MyObj *obj; 
    obj = [data objectAtIndex:rowIndex]; 
    NSString *identifier; 
    identifier = [aTableColumn identifier]; 
    if ([identifier isEqualTo:@"checked"]) { 
     BOOL value = [anObject boolValue]; 
     obj.checked = value; 
     [data replaceObjectAtIndex:rowIndex withObject:obj]; 
    } 

    [table reloadData]; 
} 

@end 

而且我從-[NSButtonCell setObjectValue]方法提出objc_msgSend_vtable5

+1

你在哪一行得到錯誤?另外,假設'MyObj'顯然是可變的 - 你設置了'checked'屬性 - 你不需要在'data'數組中替換它。您已經修改了'data'中已有的。 (在一個單獨的主題上,我建議'[obj valueForKey:標識符]'而不是'[obj performSelector:NSSelectorFromString(標識符)]'。) –

+1

我在'-tableView:setObjectValue:forTableColumn:row:'後出現錯誤處理。我只在數組中使用替換對象只是爲了可能修復這個錯誤。這不是工程。 :) – nornad

+1

這個' - [NSButtonCell setObjectValue]'方法在'-tableView:setObjectValue:forTableColumn:row:'結束之後由Cocoa框架運行。 – nornad

回答

2

我找到了我的問題的解決方案。我將BOOL checked更改爲NSNumber *checked

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { 
    MyObj *obj = (MyObj *)[data objectAtIndex:rowIndex]; 
    NSString *identifier; 
    identifier = [aTableColumn identifier]; 

    if ([identifier isEqualTo:@"checked"]) { 
     [obj setValue:[[NSNumber numberWithBool:[anObject boolValue]] retain] forKey:[aTableColumn identifier]]; 
    } 

    [table reloadData]; 
} 

而現在所有的工作都很好。我希望它能幫助別人。

+0

我的tableView:setObjectValue ...方法根本沒有被調用。我在蘋果的頭文件中發現了一條評論,說這個方法只針對基於單元格的表格視圖而不是基於視圖的表格視圖。我知道基於單元格的表格視圖已被棄用。我想使用基於視圖的表格視圖,但我不知道該如何做我的表格視圖可編輯。 – Kaydell