reloadData not working

Discussion in 'Mac Programming' started by Sander, Jul 24, 2008.

  1. Sander macrumors 6502

    Apr 24, 2008
    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:

    	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!
  2. MrFusion macrumors 6502a

    Jun 8, 2005
    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.
  3. Sander thread starter macrumors 6502

    Apr 24, 2008
    That was indeed the problem, thanks! I still need to get used to the fact that things like this don't simply crash.
  4. HiRez macrumors 603


    Jan 6, 2004
    Western US
    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.

Share This Page