PDA

View Full Version : reloadData not working




Sander
Jul 24, 2008, 05:28 AM
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:


-(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
Jul 27, 2008, 08:48 AM
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
Jul 29, 2008, 03:16 PM
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
Jul 29, 2008, 07:18 PM
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.