NSTableView error: preparedCellAtColumn:row: was Called - macos

I'm having an issue where when you select a top level object in an NSOutlineView, an error message is generated saying:
"View Based NSTableView error: preparedCellAtColumn:row: was called. Please log a bug with the backtrace from this log, or stop using the method."
The NSOutlineView I am using is set to View Based. I have no idea why the preparedCellAtColumn method is even being called. I added the method and placed a breakpoint to try and trace what is calling it, but XCode looks to be blocking the execution of it when it fires this exception.
Edit - Delegate and DateSource Methods
- (BOOL) itemAtIndexIsHeader: (NSInteger) index
{
return [self isHeader: [_projectPane itemAtRow: index]];
}
- (BOOL) isHeader: (id) item
{
return [item isKindOfClass: [Folder class]];
}
- (BOOL) outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
return NO;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
//item is nil when the outline view wants to inquire for root level items
if (item == nil)
return [[[PMDataManager sharedManager] allFolders] objectAtIndex: index];
else{
Folder *folder = (Folder *) item;
return [[[folder projects] allObjects] objectAtIndex: index];
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
return [self isHeader: item];
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if (item == nil) { //item is nil when the outline view wants to inquire for root level items
return [[[PMDataManager sharedManager] allFolders] count];
}
else if ([self isHeader: item]) {
Folder *folder = (Folder *) item;
return [[[folder projects] allObjects] count];
}
return 0;
}
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([self isHeader: item]){
PMProjectHeaderCell *cell = [outlineView makeViewWithIdentifier:#"HeaderCell" owner:self];
Folder *folder = (Folder *) item;
[[cell headerText] setStringValue: [folder name]];
return cell;
}
else{
PMProjectCell *cell = [outlineView makeViewWithIdentifier:#"ProjectCell" owner:self];
Project *project = (Project *) item;
[[cell projectNameTextField] setStringValue: [project name]];
return cell;
}
return nil;
}
- (void) outlineViewSelectionDidChange:(NSNotification *)notification
{
selectedProjectIndex = [_projectPane selectedRow];
[self reloadRightPane];
[self refresh: nil];
}

Verify that your table view content mode matches the datasource/delegate methods you are trying to use.
If you're trying to use cell-based datasource, verify that the table view content mode is "Cell Based". The same for view based.

Related

How to force redrawing a group row in a NSOutlineView after it’s been expanded/collapsed

I tried calling setNeedsDysplay: and setNeedsDisplayInRect: but drawBackgroundInRect: and drawSeparatorInRect: are not called for the group row but for everything else
This is piece of code I have:
- (void)outlineView:(NSOutlineView *)outlineView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
id prevItem = [outlineView itemAtRow:row - 1];
if ([self outlineView:outlineView isGroupItem:prevItem]) {
id view = [self outlineView:outlineView rowViewForItem:prevItem];
if (view) {
[view setNeedsDisplay:YES];
}
}
}
Edit
This is what I came up with:
- (void)outlineView:(NSOutlineView *)outlineView didRemoveRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
// TODO: find a be a better way to repaint a node after it's been collapsed
for (NSView *view in outlineView.subviews) {
NSInteger testRow = [outlineView rowForView:view];
if (testRow == row - 1) {
id item = [outlineView itemAtRow:testRow];
if ([(id)outlineView outlineView:outlineView isGroupItem:item]) {
[view setNeedsDisplay:YES];
}
}
}
}
- (void)outlineView:(NSOutlineView *)outlineView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
// TODO: find a be a better way to repaint a node after it's been expanded
for (NSView *view in outlineView.subviews) {
NSInteger testRow = [outlineView rowForView:view];
if (testRow == row - 1) {
id item = [outlineView itemAtRow:testRow];
if ([(id)outlineView outlineView:outlineView isGroupItem:item]) {
[view setNeedsDisplay:YES];
}
}
}
}
...but it's too slow in some cases
Ok, this did the trick:
- (void)outlineViewItemDidCollapse:(NSNotification *)notification {
NSInteger row = [_outlineView rowForItem:notification.userInfo[#"NSObject"]];
NSView *rowView = [_outlineView rowViewAtRow:row makeIfNecessary:NO];
[rowView setNeedsDisplay:YES];
}
- (void)outlineViewItemDidExpand:(NSNotification *)notification {
NSInteger row = [_outlineView rowForItem:notification.userInfo[#"NSObject"]];
NSView *rowView = [_outlineView rowViewAtRow:row makeIfNecessary:NO];
[rowView setNeedsDisplay:YES];
}

NSOutlineView (Source List) EXC_BAD_ACCESS error with ARC

I've an ARC enabled project and within IB I've created a window that holds the source list component which I believe is just a configured NSOutlineView. I'm using the magical delegate method:
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
for which I cannot find any documentation for at all. Once this method is implemented the root node in my outline view will appear, upon which my entire model gets deallocated. Then when I try and expand the root node the app immediately crashes as model no longer exists.
If I don't use this method, my model remains, the source list works but none of the cells appear (understandably). I'm really not doing any thing fancy here at all.
I've never run into this sort of issue with ARC before, but it's late so there is a chance I've done something dumb and just can't see it. Here's the full code:
#implementation RLListController
- (void)awakeFromNib
{
RLPerson *stan = [[RLPerson alloc] initWithName:#"Stan"];
RLPerson *eric = [[RLPerson alloc] initWithName:#"Eric"];
RLPerson *ken = [[RLPerson alloc] initWithName:#"Ken"];
RLPerson *andrew = [[RLPerson alloc] initWithName:#"Andrew"];
RLPerson *daniel = [[RLPerson alloc] initWithName:#"Daniel"];
RLPerson *aksel = [[RLPerson alloc] initWithName:#"Aksel"];
[stan addChild:eric];
[stan addChild:ken];
[stan addChild:andrew];
[ken addChild:daniel];
[daniel addChild:aksel];
self.people = [#[stan] mutableCopy];
}
#pragma mark - Source List dataSource
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
RLPerson *person = item;
return (item != nil) ? [person.children count] : [self.people count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
RLPerson *person = item;
return (item != nil) ? [person.children count] > 0 : YES;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
RLPerson *person = item;
return (item != nil) ? [person.children objectAtIndex:index] : [self.people objectAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
RLPerson *person = item;
return person.name;
}
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
RLPerson *person = item;
NSTableCellView *cell = [outlineView makeViewWithIdentifier:#"DataCell" owner:self];
cell.objectValue = person;
[cell.textField setStringValue:person.name];
return cell;
}
#end
#implementation RLPerson
- (id)initWithName:(NSString *)name
{
self = [super init];
if(self)
{
_name = [name copy];
_children = [[NSMutableArray alloc] initWithCapacity:0];
}
return self;
}
- (void)addChild:(RLPerson *)child
{
[_children addObject:child];
}
- (void)dealloc
{
NSLog(#"dealloc");
}
#end
I've just figured out a similar crash in my code. I'll describe what the cause was for me... I'm pretty sure the same applies here, but I haven't tested your code.
Be aware that awakeFromNib can be called multiple times if you have multiple NIBs. I believe this is the case if you have NSTableCellView objects embedded within the NSOutlineView within your XIB file, which are extracted when you call makeViewWithIdentifier:owner: within outlineView:viewForTableColumn:item:.
Because you are creating your model objects (stan etc) within awakeFromNib, they are being recreated during these multiple calls. With each call, ARC is cleaning up the previous model objects, but NSOutlineView is still referencing them, hence the later crash when NSOutlineView tries to ask them for more information.
The fix is to move the model object creation out of awakeFromNib, perhaps into an init method instead.
Update:
Some other small points... it also took me a while to find the documentation for the magic outlineView:viewForTableColumn:item: method. For some reason, it is part of the NSOutlineViewDelegate protocol, not NSOutlineViewDataSource. I believe that if you implement this method, you don't need an implementation of outlineView:objectValueForTableColumn:byItem:.

Row text moves up when row is deselected in NSOutlineView source list

in my app I have set up an NSOutlineView which only displays categories as a sort of menu.
This works just fine but there is a problem when I deselect an item, the titles in each row move up like in the image below:
Her is the code I am using:
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return [sectionsArray count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return NO;
}
-(BOOL)outlineView:(NSOutlineView*)outlineView isGroupItem:(id)item {
return NO;
}
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSTableCellView *cellView = [outlineView makeViewWithIdentifier:#"DataCell" owner:self];
cellView.imageView.image = [NSImage imageNamed:#"Point.png"];
cellView.textField.stringValue = item;
SectionCellCountField *number = [[SectionCellCountField alloc] initWithFrame:NSMakeRect(cellView.frame.size.width-50, 4, 35, 16)];
[cellView addSubview:number];
int rows = 0;
for (int i = 0; i < [dataArray count]; i++) {
if ([item isEqualToString:[[dataArray objectAtIndex:i]valueForKey:#"Cathegory"]]) {
rows = rows +1;
}
}
number.stringValue = [NSString stringWithFormat:#"%i", rows];
return cellView;
}
- (id)outlineView:(NSOutlineView *)outlineView
child:(NSInteger)index
ofItem:(id)item {
return [sectionsArray objectAtIndex:index];
}
Anybody have any idea why this is behavior is occurring?
Thanks a lot!
Are you using autolayout? I've seen this sort of thing happen when I forgot to constrain at least two of a table cell view subview's top, bottom, or height. Something about table and outline views tends to make ambiguous layouts act up.

TableViewController Vs UIViewController and a tableView inside

I have a Master/Details Application.By default, we have the MasterViewController attached to a TableViewController, in addition, i have attached it to an Sqlite database and all the data are showing correctly as they should. So i added a UISearchBar, in order to search upon all the items ; Search functionality's were working fine but the only bug was, the search bar disappears when scrolling down. So as a solution, i removed the TableViewController and created a simple UIVIewController and added a TableView , TableViewCell and a search bar in order to keep the search bar fix on top of the View as suggested by many people.Now the difference between those two concepts is, the TableViewController (First Case) loads the whole data in the cells once the application loads, when the ViewController ( Second Case , with a tableView , tableViewCell and a searchBar) does not load anything at startup, It loads the different elements when the user start writing a word in the searchBar. How can i force the tableView to load all Elements at startup, just like if you have a TableViewCOntroller?
This is my code :
//
// MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#import <sqlite3.h>
#import "Author.h"
#interface MasterViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
#end
#implementation MasterViewController
NSString *authorNAme , *authorNAme2;
#synthesize tableView;
#synthesize searchWasActive;
#synthesize detailViewController = _detailViewController;
#synthesize fetchedResultsController = __fetchedResultsController;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize theauthors;
NSString *PathDB;
-(BOOL)canBecomeFirstResponder{
return YES;
}
- (void)awakeFromNib
{
// self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
[super awakeFromNib];
}
- (void)viewDidLoad
{
// [self numberOfSectionsInTableView:tableView];
//[self tableView:tableView willDisplayCell:[self.tableView dequeueReusableCellWithIdentifier:#"Cell"] forRowAtIndexPath:0];
//[self.tableView
searchBar.delegate = (id)self;
// [self.tableView reloadData];
// [self configureCell:#"Cell" atIndexPath:0];
[self authorList];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)viewDidUnload
{
[self setTableView:nil];
tableView = nil;
searchBar = nil;
[self setSearchWasActive:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:#"timeStamp"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//[self authorList];
// [filteredTableData release];
//[self.tableView reloadData];
NSLog(#"Cancel Button Clicked");
// Scroll to top
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
self.searchDisplayController.searchBar.text =#" ";
}
- (void) searchBarTextDidBeginEditing:(UISearchBar *)sender
{
[self.tableView reloadData];
searchBar.showsCancelButton=NO;
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
searchBar.showsCancelButton=NO;
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = true;
filteredTableData = [[NSMutableArray alloc] init];
for (Author* author in theauthors)
{ //[NSPredicate predicateWithFormat:#"SELECT * from books where title LIKE %#", searchBar.text];
NSRange nameRange = [author.name rangeOfString:text options:NSAnchoredSearch];
NSRange descriptionRange = [author.genre rangeOfString:text options:NSAnchoredSearch];
if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
{
[filteredTableData addObject:author];
NSLog(#"Item Added is %#" , author.name);
}
}
}
[self.tableView reloadData];
}
#pragma mark - Table View methods
-(NSMutableArray *) authorList{
[self numberOfSectionsInTableView:tableView];
theauthors = [[NSMutableArray alloc] initWithCapacity:1000000];
// NSMutableArray * new2 = [[NSMutableArray alloc ] initWithCapacity:100000];
// authorNAme = theauthors.sortedArrayHint.description;
#try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSLog(#"Before the dbpath variable");
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:#"dictionary_native.sqlite"];
NSLog(#"After the dbpath variable");
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(#"1");
NSLog(#"Cannot locate database file '%#'.", dbPath);
}
NSLog(#"Database correctly located");
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(#"2");
NSLog(#"An error has occured: %#", sqlite3_errmsg(db));
}
NSLog(#"Database correctly opened");
// const char *sql = "SELECT F_Keyword FROM wordss";
const char *sql = "SELECT * FROM wordss";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(#"Problem with prepare statement: %#", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
// NSLog(#"entered the while statement");
Author * author = [[Author alloc] init];
// // NSLog(#"Author initialised");
//
author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
// NSLog(#"this is the author.name %#" , author.name);
author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
//
// authorNAme=author.genre;
//
[theauthors addObject:author];
}
// authorNAme = author.genre;
}
}
#catch (NSException *exception) {
NSLog(#"Problem with prepare statement: %#", sqlite3_errmsg(db));
}
#finally {
// sqlite3_finalize(sqlStatement);.
// authorNAme = nil;
// authorNAme = Nil;
return theauthors;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// return [[self.fetchedResultsController sections] count];
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
// return [sectionInfo numberOfObjects];
int rowCount;
if(self->isFiltered)
rowCount = filteredTableData.count;
else
rowCount = theauthors.count;
NSLog(#"This is the number of rows accepted %i" , rowCount);
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
int rowCount = indexPath.row;
Author *author = [self.theauthors objectAtIndex:rowCount];
if(isFiltered){
author = [filteredTableData objectAtIndex:indexPath.row];
}
else{
author = [theauthors objectAtIndex:indexPath.row];
}
cell.textLabel.text = author.name;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// The table view should not be re-orderable.
return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// self.detailViewController.detailItem = object;
// DetailViewController* vc ;
MasterViewController *author;
NSLog(#"This is the showDetailsForIndexPath");
[self->searchBar resignFirstResponder];
// Details* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Details"];
// AuthorVC* author;
if(isFiltered)
{
author = [filteredTableData objectAtIndex:indexPath.row];
}
else
{
author = [theauthors objectAtIndex:indexPath.row];
}
//vc.author = author;
authorNAme = author.genre;
authorNAme2 = author.name ;
// author = [theauthors objectAtIndex:indexPath.row];
NSLog(#"This is the author.genre %#" , author.genre);
//vc.author.genre = author.genre;
authorNAme = author.genre;
authorNAme2 = author.name;
//NSLog(#"This is the details %#",vc.author.genre);
NSLog(#"This is the authorNAme Variable %#" , authorNAme);
self.detailViewController.detailItem = authorNAme;
self.detailViewController.detailItem2 = authorNAme2;
}
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"timeStamp" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
// UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
/*
// Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
// In the simplest, most efficient, case, reload the table view.
[self.tableView reloadData];
}
*/
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
// NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
// cell.textLabel.text = [[object valueForKey:#"timeStamp"] description];
}
#end
Any help Will be highly appreciated...Thank you for taking the time.
If the tableview is in xib link it to correct object and set delegate and datasource.

Why are my table view delegate methods not being called?

I'm building a document based Mac application. I have two classes myDocument and Person. The difficulty I'm having is when I push the button to add a new Person in the table view and display it it doesn't show in the table view. I've placed log statements inside the delegate methods. Since my log statements are not being displayed in the console I know they are not being called. Here are the implementations of the delegate methods
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [employees count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
// What is the identifier for the column?
NSString *identifier = [aTableColumn identifier];
NSLog(#"the identifier's name is : %s",identifier);
// What person?
Person *person = [employees objectAtIndex:rowIndex];
// What is the value of the attribute named identifier?
return [person valueForKey:identifier];
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
Person *person = [employees objectAtIndex:rowIndex];
NSLog(#"inside the setObjectMethod: %#",person);
// Set the value for the attribute named identifier
[person setValue:anObject forKey:identifier];
[tableView reloadData];
}
Here is a pic of my .xib
Here are my actions methods
#pragma mark Action Methods
-(IBAction)createEmployee:(id)sender
{
Person *newEmployee = [[Person alloc] init];
[employees addObject:newEmployee];
[newEmployee release];
[tableView reloadData];
NSLog(#"the new employees name is : %#",[newEmployee personName]);
}
-(IBAction)deleteSelectedEmployees:(id)sender
{
NSIndexSet *rows = [tableView selectedRowIndexes];
if([rows count] == 0){
NSBeep();
return;
}
[employees removeObjectAtIndexs:rows];
[tableView reloadData];
}
You forgot to bind the document's tableView outlet to the actual table view. Thus your reloadData messages are sent to nil.

Resources