NSAssertionFailure upon launch with view based table

Originator:brettper
Number:rdar://10925941 Date Originated:24/2/2012
Status:Closed Resolved:Duplicate (10925830)
Product:Mac OS SDK Product Version:10.7.3
Classification:Serious Bug Reproducible:Always
 
Summary:

A simple application using the same NSTableViewDataSource methods as the basic table view in 

Steps to Reproduce:

1. Create new Cocoa application in Xcode with default settings (non-document, no ARC etc)
2. Drag NSTableView to window in MainMenu.xib
3. Set table view's content mode to View Based
4. Connect table view's datasource and delegate to app delegate in MainMenu.xib
5. Add tableView property to app delegate
6. Add simple numberOfRowsInTableView: and tableView:viewForTableColumn:row: methods as shown in attached sample project
7. Set table view NSTableCellView identifier to 'MainCell'

The table view works fine up to this point.  When run, the datasource contents are shown

8. Connect tableView property from app delegate to table view in MainMenu.xib

Program now crashes with assertion failure.  Crash can be reversed by removing connection from app delegate to tableView

Expected Results:
data from content array displayed in table

Actual Results:

*** Assertion failure in -[NSTableRowData _addRowViewForVisibleRow:withPriorView:], /SourceCache/AppKit/AppKit-1138.32/TableView.subproj/NSTableRowData.m:2484
2012-02-24 21:52:47.722 BrokenVBTV[87289:403] An uncaught exception was raised
2012-02-24 21:52:47.723 BrokenVBTV[87289:403] Row 1 should be in the valid visible section
2012-02-24 21:52:47.737 BrokenVBTV[87289:403] (
	0   CoreFoundation                      0x00007fff94e8dfc6 __exceptionPreprocess + 198
	1   libobjc.A.dylib                     0x00007fff8ffe9d5e objc_exception_throw + 43
	2   CoreFoundation                      0x00007fff94e8ddfa +[NSException raise:format:arguments:] + 106
	3   Foundation                          0x00007fff966c4743 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 169
	4   AppKit                              0x00007fff9522b0f7 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 164


Regression:

Notes:


--------
Header file

#import <Cocoa/Cocoa.h>

@interface TLAAppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate>
{
	NSMutableArray *contentArray;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTableView *tableView;

- (UInt) numberOfRowsInTableView: (NSTableView *) tableView;
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;


0-----

Implementation file

#import "TLAAppDelegate.h"

@implementation TLAAppDelegate

@synthesize window = _window;
@synthesize tableView = _tableView;

- (void)dealloc
{
	[contentArray release];
	
    [super dealloc];
}

- (void) awakeFromNib;
{
	contentArray = [[NSArray arrayWithObjects: @"First Thing", @"Second Thing", @"Third Thing", nil] retain];
	[_tableView reloadData];
}

- (UInt) numberOfRowsInTableView: (NSTableView *) tableView;
{
	NSLog (@"Count: %li", [contentArray count]);
	return [contentArray count];
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
	
	NSLog (@"row %li %@", row, [contentArray objectAtIndex: row]);
	
	NSTableCellView *cellView = [tableView makeViewWithIdentifier: @"MainCell" owner:self];
	cellView.textField.stringValue = [contentArray objectAtIndex: row];
	return cellView;
}

Comments

Reduction of bug

Seems the actual issue is calling -reloadData on the table view in -awakeFromNib. If I don't do that (but call reloadData on other occasions when the data changes), the table works fine, even when an outlet is connected to it. It also makes much more sense, because connecting an outlet just increments the retain count. Not setting the outlet just turns that line into a NIL-message, which fixes the issue, too.

I've blogged the solution I've found here: http://orangejuiceliberationfront.com/view-based-nstableviews-row-1-should-be-in-the-valid-visible-section/


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!