Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sander

macrumors 6502a
Original poster
Apr 24, 2008
521
67
Hi all,

I am trying to pick up Cocoa programming. I posted the following problem on the CocoaDev site but didn't really get any useful hints.

As an exercise, I wrote a Sudoku-solving program. It uses an NSTableView which I set up in Interface Builder to show a headerless, 9x9 view with number formatters. I have an AppController object which acts as the dataSource for the NSTableView, and which holds a 2D array of data (I know about bindings - I just wanted to do this exercise "manually"). In Interface Builder, I gave each table column an identifier (the numbers 0 - 8). In tableView: objectValueforTableColumn:row I do the following:

Code:
-(id)tableView:(NSTableView*)aTableView
objectValueForTableColumn:(NSTableColumn*)aTableColumn
		   row:(int)rowIndex
{
	NSString *identifier = [aTableColumn identifier];
	int colIndex = [identifier intValue];
	if (m_values[rowIndex][colIndex])
		return [NSNumber numberWithInt:m_values[rowIndex][colIndex]];
	return nil;
}

(I don't really like this hack to get at the column number. On CocoaDev, someone suggested I shouldn't be using an NSTableView but rather an NSMatrix. I concur and will change my program accordingly. For now, I just want to understand why reloadData is not working correctly - see below.)

I also implemented tableView:setObjectValue:forTableColumn. Everything works fine so far.

I wanted to implement loading and saving of the sudoku grids, and decided to do so using "plain" C code. However, when I read in the values and change my m_values array and do a [m_gridView reloadData], the grid is not updated on screen. When I select a row in the table, it is immediately filled with the correct (new) data, but I would expect that the reloadData would trigger a redraw automatically.

I tried adding [m_gridView setNeedsDisplay:TRUE] and even [[m_gridView window] displayIfNeeded] but to no avail.

I have no idea why my NSTableView is not redisplaying the correct values automatically. All hints are welcome!
 

MrFusion

macrumors 6502a
Jun 8, 2005
613
0
West-Europe
Hi all,

I wanted to implement loading and saving of the sudoku grids, and decided to do so using "plain" C code. However, when I read in the values and change my m_values array and do a [m_gridView reloadData], the grid is not updated on screen. When I select a row in the table, it is immediately filled with the correct (new) data, but I would expect that the reloadData would trigger a redraw automatically.

I tried adding [m_gridView setNeedsDisplay:TRUE] and even [[m_gridView window] displayIfNeeded] but to no avail.

I have no idea why my NSTableView is not redisplaying the correct values automatically. All hints are welcome!

Did you connect the IBOutlet in InterfaceBuilder for you NSTableView?
Apparently you did connect the delegate, since it's fetching data, but actions you send to it are not received if you don't connect the outlet.
 

Sander

macrumors 6502a
Original poster
Apr 24, 2008
521
67
Did you connect the IBOutlet in InterfaceBuilder for you NSTableView?
Apparently you did connect the delegate, since it's fetching data, but actions you send to it are not received if you don't connect the outlet.

That was indeed the problem, thanks! I still need to get used to the fact that things like this don't simply crash.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
That was indeed the problem, thanks! I still need to get used to the fact that things like this don't simply crash.
Often that's because in Objective-C, unlike most other languages, it's not an error to send messages to a nil pointer, it's a no-op. Personally, I like that, but it can make debugging a little trickier sometimes because as you say, it's not always obvious when something's wrong.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.