• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSTableView, bindings & undo - "ToDos" example - undo bug
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSTableView, bindings & undo - "ToDos" example - undo bug


  • Subject: NSTableView, bindings & undo - "ToDos" example - undo bug
  • From: George Lawrence Storm <email@hidden>
  • Date: Thu, 28 Oct 2004 13:25:16 -0700

I am experimenting with the "ToDos" example from http://homepage.mac.com/mmalc/CocoaExamples/controllers.html

I am having a problem adding Undo, I have modified the "ToDos" example as shown below.

Modified MyDocument.h as follows:

Added
IBOutlet NSTableView *myTableView;
and connected it in IB.

Modified MyDocument.m as follows:

- (void)insertObject:(id)anObject inToDosAtIndex:(unsigned int)index
{
// Add the inverse of this operation to the undo stack
NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self]
removeObjectFromToDosAtIndex:index];
if (![undo isUndoing])
[undo setActionName:@"Insert To Do"];

// Add the "To Do" to the array
[toDos insertObject:anObject atIndex:index];
}

- (void)removeObjectFromToDosAtIndex:(unsigned int)index
{
NSDictionary *dictionary = [toDos objectAtIndex:index];

// Add the inverse of this operation to the undo stack
NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self]
insertObject:dictionary inToDosAtIndex:index];
if (![undo isUndoing])
[undo setActionName:@"Delete To Do"];

// Remove the "To Do" from the array
[toDos removeObjectAtIndex:index];

// ***** The following line fixes all but the
// ***** case of a single row existing before the undo
[myTableView selectRowIndexes:[NSIndexSet
indexSetWithIndex:([self countOfToDos] - 1)]
byExtendingSelection:NO];
}

When I -removeObjectFromToDosAtIndex: then undo, there is no problem,
however when I -insertObject:inToDosAtIndex: then undo, I get the following error:
2004-10-28 12:49:04.650 ToDos[1413] *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)
(error shown occured after I leave my -removeObjectFromToDosAtIndex: method, the error shown is the result of inserting twice and undoing once, error shown is prior to my adding the last line shown in -removeObjectFromToDosAtIndex: shown above)
Once I encounter this error subsiquent calls to undo generate the following error:
2004-10-28 12:49:06.135 ToDos[1413] undo: NSUndoManager 0x582850 is in invalid state, undo was called with too many nested undo groups

I noticed that if I -insertObject:inToDosAtIndex:, then selected an earlier row before I undo I do not get the error.
Thinking myTableView was not noting the number of rows had changed, or that it stayed selected beyond the end of its range.
I attempted to add [myTableView noteNumberOfRowsChanged]; at the end of my -removeObjectFromToDosAtIndex: with no difference in the results.
I also tried adding [myTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:([self countOfToDos] - 1)] byExtendingSelection:NO]; this works in all cases except if there is only one row remaining before the undo, in which case I get the error.

Is there a better way of doing this?

-----

George Lawrence Storm
Macintosh Applications Development
Snohomish (Seattle), Washington
E-mail: <email@hidden>

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Prev by Date: Is this normal (memory allocation)
  • Next by Date: OT: Chicago Tiger Tech Talk
  • Previous by thread: Re: Is this normal (memory allocation)
  • Next by thread: OT: Chicago Tiger Tech Talk
  • Index(es):
    • Date
    • Thread