PDA

View Full Version : tableview methods only called when returning a number and not the numeric result of a




NSNick
Nov 25, 2011, 11:36 PM
Why does this work

#import "ArduSerialAppDelegate.h"

@implementation ArduSerialAppDelegate

@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

arduino = [[Matatino alloc] initWithDelegate:self];
NSLog(@"%@", [arduino deviceNames]);
devices = [[NSMutableArray alloc] init];
[devices addObjectsFromArray:[arduino deviceNames]];
NSLog(@"devices %@", devices);
devicesTable = [[NSTableView alloc] init];
[devicesTable setDataSource:self];
[devicesTable setDelegate:self];
//[devicesTable reloadData];
}

-(void)awakeFromNib
{
[devicesTable reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return 2;
}

- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
NSLog(@"objectValueForTableColumn");
NSLog(@"devices inside objectvalue%@", [devices objectAtIndex:row]);
return [[arduino deviceNames] objectAtIndex:row];
}

- (void) receivedString:(NSString *)rx {
}
- (void) portAdded:(NSArray *)ports {
}
- (void) portRemoved:(NSArray *)ports {
}
- (void) portClosed {
}

@end

and this does not

#import "ArduSerialAppDelegate.h"

@implementation ArduSerialAppDelegate

@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

arduino = [[Matatino alloc] initWithDelegate:self];
NSLog(@"%@", [arduino deviceNames]);
devices = [[NSMutableArray alloc] init];
[devices addObjectsFromArray:[arduino deviceNames]];
NSLog(@"devices %@", devices);
devicesTable = [[NSTableView alloc] init];
[devicesTable setDataSource:self];
[devicesTable setDelegate:self];
//[devicesTable reloadData];
}

-(void)awakeFromNib
{
[devicesTable reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [[arduino deviceNames] count];
}

- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
NSLog(@"objectValueForTableColumn");
NSLog(@"devices inside objectvalue%@", [devices objectAtIndex:row]);
return [[arduino deviceNames] objectAtIndex:row];
}

- (void) receivedString:(NSString *)rx {
}
- (void) portAdded:(NSArray *)ports {
}
- (void) portRemoved:(NSArray *)ports {
}
- (void) portClosed {
}

@end


- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
is only called when
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
returns a number and not the result of a method?



chown33
Nov 26, 2011, 11:39 AM
The only difference in your posted code is this, from the non-working one:
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [[arduino deviceNames] count];
}

It would have been nice if you'd hilited that, instead of forcing us to diff the code.

The problem is probably that arduino is nil, or the deviceNames method is returning nil, or count is simply 0 for some other reason. You might want to add some more specific NSLog's in applicationDidFinishLaunching:. Or use the debugger and inspect the values.

If arduino or deviceNames are nil, or count is 0, make sure the objects have a chance to set up some actual device names before the count is returned. That is, the reason for nil or 0 could be that the newly created Matatino object hasn't had a chance to do anything yet, like load device names into its array.

Looking at your other code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

arduino = [[Matatino alloc] initWithDelegate:self];
NSLog(@"%@", [arduino deviceNames]);
devices = [[NSMutableArray alloc] init];
[devices addObjectsFromArray:[arduino deviceNames]];
NSLog(@"devices %@", devices);
devicesTable = [[NSTableView alloc] init];
[devicesTable setDataSource:self];
[devicesTable setDelegate:self];
//[devicesTable reloadData];
}

I have to ask what the NSLog's printed out.

It's also unclear why you'd want the instance-variable devices to be a separate array from the [arduino deviceNames] array. Making duplicates of data without a reason is a good way to get confused about which data is the real data. I mention this because the instance-variable devices isn't used as the data-source for the table view. The actual arduino deviceNames is.

Finally, I don't see any code that adds devicesTable to any view. This strongly suggests that devicesTable isn't actually visible on the screen. It might also mean you haven't posted that code, but if the posted code is complete, and you don't add devicesTable as a subview, then what you seem to think is happening probably isn't. In particular, devicesTable will never be the tableView parameter of any of the data-source protocol's methods.