简体   繁体   中英

NSTableView edits not changing NSMutableArray

I have a Class that stores some strings along with a BOOL flag that is set to YES if one of the strings has been updated.

I have a NSTableView that displays the strings in my class. The view is controlled via separate controller class and the view is fed by an NSMutableArray.

The GUI stuff seems to work fine in terms of displaying data and allowing me to edit the cells in the table view. The problem I am having is the edits don't change the objects stored in the NSMutableArray. I have some debug code to print out the strings when I close the app, and none of the changes made in the GUI show up in the objects at this point. Setting a break point where those values are changed shows me the objects are indeed changed, but those changes seem to get lost. How can I get any changes I make in the view to persist in the object stored in the NSMutableArray backing the NSTableView?

Here is how I am coding:

// my class .h file

@interface Snip : NSObject <NSMutableCopying>

@property (assign) int64_t  id_num;
@property (assign) BOOL     changed;
@property NSMutableString   *name;
@property NSMutableString   *text;
@property (copy) NSString   *language;


// my class .m file

import "Snip.h"

@implementation Snip

@synthesize id_num;
@synthesize name;
@synthesize text;
@synthesize changed;
@synthesize language;

method from my controller class

// edit table values
-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn(NSTableColumn *)tableColumn row:(NSInteger)row
{
  Snip *sn = [snippet_list objectAtIndex:row];

  [sn setChanged:YES];

  [sn setValue:object forKey:[tableColumn identifier]];

  NSLog(@"Change: %@",sn.name);
}

That last NSLog statement displays the change I made in the GUI. Also, the setChanged:YES is lost as well when I print the NSMutableArray contents when closing the app.

This behaviour is often due to the data being initialised again at some point after being changed by the user.

You can catch this problem by setting a breakpoint (or NSLog statement) in your initialisation code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM