0

I have been at this for a few days now and cant figure this out.

So, say I have an array:

Item # | Name | Price | Discount
1      | Car  | 3000  | 15%
2      | Bus  | 6000  | 5%
3      | .... | 4500  | 0

Also I have a table view where I want items to show like the scheme above, but instead what i get is:

1     |  1
Car   |  Car
Bus   |  Bus
....  |  ...

So the tableview actually repeats only the first row of the Array and prints its contents repeatedly for each consecutive column.

I am just beginning to code so would be nice to hear some advice. The tableView is linked to sourceData, and made Outlet.

I have the following overrides:

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [self.allData count];
}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    return [self.allData objectAtIndex:row];
}

-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    [self.allData replaceObjectAtIndex:row withObject:object];
    [self updateChangeCount:NSChangeDone];

}

What am I missing?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Dmitri K.
  • 197
  • 1
  • 13
  • http://stackoverflow.com/questions/22375689/automatic-table-column-indentifier-in-nstableview – Oscar Apeland Oct 25 '15 at 12:21
  • You're missing that you need to select a index in the row of your array, so you get the correct X and Y of your data. – Oscar Apeland Oct 25 '15 at 12:22
  • `-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { if ([tableView tableColumns][0] == tableColumn) { return [NSString stringWithFormat:@"%@", self.allData[row][0]]; } else if ([tableView tableColumns][1] == tableColumn) { return [NSString stringWithFormat:@"%@", self.allData[row][1]]; } return nil; }` – Dmitri K. Oct 25 '15 at 12:57
  • Changed to the above, dont get why it is formatted like this, but anyways. Now my array is workin as it was. its name is allData. Buth when i try to open the tableView I get an exception. unrecognized selector sent to instance 0x608000029140. also 5 Photography 0x0000000100002c79 -[Document tableView:objectValueForTableColumn:row:] + 297 I expect i did something terribly wrong here, all my data is stored as NSStrings in array. – Dmitri K. Oct 25 '15 at 12:59
  • I am not good with OSX stuff, but i will try to get this working for ya when i get home. Expect an answer in about 10 hours. – Oscar Apeland Oct 26 '15 at 08:36
  • Is the data a NSArray of NSStrings or a NSArray of NSArray of NSStrings? – Willeke Oct 26 '15 at 10:47
  • the data is NSArray containing only NSStrings – Dmitri K. Oct 26 '15 at 11:02
  • I heard there is some way to use the ArrayController or do what i need via Dictionary, but I couldnt find, and yes i have been searching for a week now, a completely explained solution. All i know is not to use Cell based tableView and use the new View Based mode. But thats pretty much it... I will wait for any possible replies, whenever they come. – Dmitri K. Oct 26 '15 at 20:12
  • Did you read the [Table View Programming Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TableView/Introduction/Introduction.html#//apple_ref/doc/uid/10000026i-CH1-SW1)? – Willeke Oct 27 '15 at 02:07
  • And take a look at [TableViewPlayground](https://developer.apple.com/library/mac/samplecode/TableViewPlayground/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010727-Intro-DontLinkElementID_2). – Willeke Oct 27 '15 at 02:11

0 Answers0