0

At the suggestion of another member, I've rewritten part of my recent project. I've now fixed the trouble I was having earlier, only to spend a few hours struggling with this one. Any/all help is much appreciated.

When I try to add more than one item to my NSTableView with a NSMutableArray, it replaces all of the entries in dateList with the most recent entry. Here's my .m file. The NSLog I have setup in "setting d object date to %@", will get called the nth time as I continue to add dates.

 @implementation TableViewController
 @synthesize myDatePicker;
 NSString *myRenamed = @"CHANGED";
 int test = 0;


 - (id) init{

self = [super init];
if (self) {
    dateList = [[NSMutableArray alloc] init];
}   
return self;
 }


 -(void) awakeFromNib{
 [myDatePicker setDateValue: [NSDate date]];
 [self.myDatePicker sendActionOn:NSLeftMouseDown];
 NSLog(@"Hello from TableView at time: %@", myDatePicker.dateValue);
 }


- (IBAction)datePickerAction:(id)sender{
 test = 0;
[self getSetDate];
NSLog(@"getSetDate Finished with date %@", myRenamed);
[self myTableAdd];
NSLog(@"datePickerAction Finished");

 }


 - (void)getSetDate{

NSLog(@"Entering getSetDate%@", (myDatePicker.dateValue));

//set date to local & set format set on xib start
NSDate *myNewDate = [myDatePicker dateValue];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYYMMdd"];
myRenamed = [dateFormat stringFromDate:myNewDate];
NSLog(@"Ending getSetDate with %@",myRenamed);




  }


 - (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView{

return [dateList count];
 }

 -(id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{

Date *d = [dateList objectAtIndex:row];
NSString *identifier = [tableColumn identifier];

d.date=myRenamed;
[d setDate:d.date];
NSLog(@"setting d object date to %@", d.date);
test ++;
return [d valueForKey:identifier];

 }


 - (void) myTableAdd{
NSLog(@"Called myTableAdd");

[dateList addObject:[[Date alloc] init]];
[tableview reloadData];

 }

Image: http://cl.ly/NaEu This is what happens, all the previous date entries are changed to the last-clicked entry. All help is appreciated.

ericdmann
  • 357
  • 5
  • 20

1 Answers1

0

In objectValueForTableColumn you're overwriting every value with the one you just added

d.date=myRenamed;

Gary
  • 5,642
  • 1
  • 21
  • 41
  • Thanks! However, if I comment that out, the dates never get assigned anything. I need them to grab the values that are set to myRenamed (a modified date). – ericdmann Mar 15 '13 at 03:35
  • Why don't you just store the newly added date in dateList and use those objects? I'm not sure what you're Date class is doing. – Gary Mar 15 '13 at 03:40
  • I'll give that a shot. I wanted to leave my options open to store an object in, but for a date nsstring would be fine. – ericdmann Mar 15 '13 at 03:43
  • Also, any idea why it would be getting called n times? – ericdmann Mar 15 '13 at 03:43
  • You call reloadData so it will reload the whole table. – Gary Mar 15 '13 at 03:51