2

我有一个基于视图的 NSTableView,它最初应该是空的。我还有“添加”和“删除”按钮,用于从同一个 NSTableView 添加和删除行。

我的委托方法和添加新行的方法如下所示:

#import "PreferencesApiKeysViewController.h"
#import "UserKeys.h"

@interface PreferencesApiKeysViewController ()

@property (weak) IBOutlet NSTableView *keysTableView;

@end

@implementation PreferencesApiKeysViewController
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    // Get a new ViewCell
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];

    if([tableColumn.identifier isEqualToString:@"userKeysColumn"]) {
        UserKeys *allKeys = [self.allKeys objectAtIndex:row];
        cellView.textField.stringValue = allKeys.userKeyName;
        return cellView;
    }
    return cellView;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    NSLog(@"Initial rows: %li", (unsigned long)[self.allKeys count]);
    return [self.allKeys count];
}

- (IBAction)addKey:(id)sender {
    UserKeys *newKey = [[UserKeys alloc] initWithKeyName:@""
                                                   apiID:@""
                                                 apiCode:@"" ];

    [self.allKeys addObject:newKey];
    NSLog(@"Total rows: %li", (unsigned long)self.allKeys.count);
    NSInteger newRowIndex = self.allKeys.count;
    if (newRowIndex == 0) {
        NSLog(@"No rows.");
        newRowIndex = 0;
    } else {
        NSLog(@"Has rows.");
        newRowIndex = self.allKeys.count-1;
    }
    NSLog(@"New Index: %ld", (long)newRowIndex);

    [self.keysTableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:newRowIndex] withAnimation:NSTableViewAnimationSlideDown];

    [self.keysTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:newRowIndex] byExtendingSelection:NO];
    [self.keysTableView scrollRowToVisible:newRowIndex];
}

@end

在我的 AppDelegate.m 中,我这样称呼我的观点:

#import "AppDelegate.h"
#import "UserKeys.h"
#include "PreferencesApiKeysViewController.h"

@interface  AppDelegate()
@property (nonatomic,strong) IBOutlet PreferencesApiKeysViewController *prefsViewController;
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.prefsViewController = [[PreferencesApiKeysViewController alloc] initWithNibName:@"PreferencesApiKeysViewController" bundle:nil];

    // Create few rows as dummy data
    /*
    UserKeys *key1 = [[UserKeys alloc] initWithKeyName:@"key one"
                                                 apiID:@"123"
                                               apiCode:@"xxx" ];

    UserKeys *key2 = [[UserKeys alloc] initWithKeyName:@"key two"
                                                 apiID:@"456"
                                               apiCode:@"yyy" ];
    NSMutableArray *tempKeys = [NSMutableArray arrayWithObjects:key1, key2, nil];
    self.prefsViewController.allKeys = tempKeys;
    */
    // done.

    [self.window.contentView addSubview:self.prefsViewController.view];
    self.prefsViewController.view.frame = ((NSView*)self.window.contentView).bounds;
}

@end

现在,这里的问题是,如果我取消注释在 AppDelegate 中添加虚拟数据的那几行并启动我的应用程序,一切正常。我可以毫无问题地添加/删除行,我什至可以删除所有行,然后再添加一个新行。

但是,如果我再次评论这些行,我的应用程序会从一个空表开始(这是我需要的),当我想添加新行时,我会收到一个错误:

*** Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1187.34/AppKit.subproj/NSCell.m

进一步看线程,我只看到一行引用了我的应用程序:

0x0000000100003116 -[PreferencesApiKeysViewController tableView:viewForTableColumn:row:] + 406

我猜如果allKeys数组真的不包含任何单个对象,我的 TableView 没有正确实例化,但我不知道如何解决这个问题?如何创建一个空的 NSTableView 并能够在单击“添加”按钮时自己添加第一行(不添加任何虚拟数据allKeys)?

4

1 回答 1

2

将此添加到的实现中PreferencesApiKeysViewController

@implementation PreferencesApiKeysViewController

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle {
    if ((self = [super initWithNibName:nibName bundle:bundle])) {
        self.allKeys = [NSMutableArray array];
    }
    return self;
}

// add the following only if not using ARC
- (void)dealloc {
    [_allKeys release];
    [super dealloc];
}

@end

当您允许编译注释掉的代码时发生的事情是您将NSMutableArray实例分配给allKeys实例变量。因此,添加和删除项目的工作方式与您预期的一样。

Generally, in a case like this, the class that manages the NSMutableArray should override the init method to make sure the array is properly initialized to an empty array. (By default, non-IBOutlet instance variables are initialized to nil, which won't allow you to properly add or remove items from it).

于 2013-02-21T14:39:29.930 回答