CocoaDev

Edit AllPages

I have a tableView, which I have subclassed in order to draw a gradient selection background. I then have a subclassed NSCell which is very similar to the example “IconAndTextCell” that Apple provides. My tableView’s dataSource method queries an NSMutableArray, and then returns values from it. Currently, when I edit my subclassed NSCell, it works as expected for the first edit. From then on, every time I edit a cell, every cell that I have edited up to this point changes its text to reflect the current cell’s new title. Does anyone see anything in this code that might be causing this?

In my tableView subclass, I have this, in order to prevent the tableView from selecting the next row when I finish editing.

And in my [[NSCell subclass, I have this:

In case anyone is wondering about this line: int indexToQueryForTitle = row * 5; I have other information about each row stored in the array too, so this finds the index to store the title in.

Does anyone see a reason why every cell I have edited up to this point would be edited to have the new title?

Also, as you can tell, my array is stored in [[MyDocument because the values and size of the array changes based on which document is in focus. Unfortunately, this throws up some warnings when I compile. Is there a better way to reference this NSMutableArray?

– MattBall

Well, two things pop out at me. First, why can’t your table view’s data source handle the text update in tableView:setObjectValue:forTableColumn:row:? (If it’s because of the textDidEndEditing: hack, I can’t think of an easy way to fix it…) Controls should not (in general) manage their own models. Probably your main problem is the selectedRow, which isn’t always that reliable (in my experience). Secondly, there is no need to create an empty NSMutableArray just to reference the other one. In fact, this code is leaking memory because of it. Either take out “ = [[NSMutableArray alloc] init]” (but leave the semicolon), or move the entire assignment on the second line into the declaration. (Shown below) Good luck… –JediKnil

NSMutableArray *layersArray = [[[NSDocumentController sharedDocumentController] currentDocument] layersArray];


Thanks, that worked perfectly. That was one of the first things that I tried when this problem cropped up, but it made things terribly worse. I suppose something I changed in my search of the problem fixed it. And thanks for the heads up about the memory leak. The only problem I’m still having is that my mutableArray reference is still giving some warnings. If anyone has any insight into that, that would be great. In MyDocument, I’ve got a reference in the header and the main file to return the array, which I thought would fix it, but it didn’t.

– MattBall

I think I figured it out… currentDocument doesn’t know what type of document is being displayed, so it will return an NSDocument. You, of course, know that it is an instance of MyDocument, but the compiler doesn’t. And there’s your warning. –JediKnil

So there’s no easy way to fix it? As long as it’s not a problem, I suppose I can just ignore it… – MattBall

The only real way to fix the warning is to add a cast to the code, like so, although the second example might be clearer. –JediKnil

NSMutableArray *layersArray = [(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument] layersArray]; // This is simpler MyDocument *current = (MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument]; NSMutableArray *layersArray = [current layersArray];


Alright, I’m back with some more editing weirdness… I was working on fixing some memory leaks, and realized that I never implemented NSCopying in my NSCell subclass. When I implemented it, my cell editing went to hell. When I double click to edit the cell, the cell’s background turns white, and the text is selected, but I can’t edit or reselect it. Also, I should mention that the entire cell should not be white. Only a small area around the text should have the FieldEditor. Here’s my code:

// Reset the text color to account for the cell being selected

// Modify the editing frame to compensate for the thumbnail image

Now, selectWithFrame: IS getting called, but it doesn’t seem to be having any effect.

– MattBall

Okay, I resolved it, thanks to the kind people on the Cocoa-Dev mailing list. It was an issue with my copyWithZone: method.

– MattBall