I'm trying to create a custom UITableViewCell, but it crashes (EXC_BAD_ACCESS) after cellForRowAtIndexPath is called.
Is there anything else required when returing a custom cell?
I'm trying to create a custom UITableViewCell, but it crashes (EXC_BAD_ACCESS) after cellForRowAtIndexPath is called.
Is there anything else required when returing a custom cell?
In the... you need to load the CustomCell you created... something like this...
should work fine...
Thanks, but I should also mention I'm not using NIBs or the Interface Builder.
This is all being done programmatically.
And the crash occurs after the method cellForRowAtIndexPath has returned, and not in any
specific place while the method is being called.
But, after much frustration, I did manage to find the bug after I turned on Zombie Objects for
first time. The bug was in my custom initWithStyle method, where I allocate both custom labels.
For some reason, I had to use "self.myLabel1 = variable", instead of "myLabel1 = variable".
I don't understand why these two lines aren't the same. I synthesized both variables:
@synthesize myLabel1;
@synthesize myLabel2;
Is there some Objective-C rule stating that I can't use synthesized variables (setters and getters)
in constructors (initializers)?
Thanks, but I should also mention I'm not using NIBs or the Interface Builder.
This is all being done programmatically.
And the crash occurs after the method cellForRowAtIndexPath has returned, and not in any
specific place while the method is being called.
But, after much frustration, I did manage to find the bug after I turned on Zombie Objects for
first time. The bug was in my custom initWithStyle method, where I allocate both custom labels.
For some reason, I had to use "self.myLabel1 = variable", instead of "myLabel1 = variable".
I don't understand why these two lines aren't the same. I synthesized both variables:
@synthesize myLabel1;
@synthesize myLabel2;
Is there some Objective-C rule stating that I can't use synthesized variables (setters and getters)
in constructors (initializers)?
Code:
self.myLabel1 = variable;
and
Code:
myLabel1 = variable;
are indeed different. The first form uses the property. That code ends up calling the setter method for the property. If myLabel1 is set up as a retained property, the setter will retain the value assigned to it.
The second form does a simple assignment to the instance variable.
When you create a property for an ivar, you should really use the property to access the variable and stop using the ivar directly.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
are indeed different. The first form uses the property. That code ends up calling the setter method for the property. If myLabel1 is set up as a retained property, the setter will retain the value assigned to it.
The second form does a simple assignment to the instance variable.
When you create a property for an ivar, you should really use the property to access the variable and stop using the ivar directly.
Thanks, but I think I still misunderstand some fundamental fact about synthesized variables.
So, you're saying I can't use them for setting?
I thought that was the reason why I synthesize them in the first place.
Thanks, but I think I still misunderstand some fundamental fact about synthesized variables.
So, you're saying I can't use them for setting?
I thought that was the reason why I synthesize them in the first place.
The @synthesize directive asks the compiler to create a getter and a setter for a property you defined. That's all it does.
The statement:
[/code]
Code:
myLabel1 = variable;
changes the value of the underlying instance variable without invoking the setter. The code
[/code]
Code:
self.myLabel1 = variable;
on the other hand, invokes the setter, and does whatever housekeeping the setter is set up to do (including retaining the new object, if the property is a retained property.)
If you create a property, you should use the self.property syntax (or the alternate [self property] and [self setProperty: value] syntax) and not refer to the instance variable directly, unless you know what you're doing and why.
Note that if you are writing a custom setter or getter (instead of using the @synthesized version) then you have to refer to the instance variable directly, to avoid endless recursion. That's an exception. There are a few more, but until you have a reason to access the instance variable directly, and know why you're doing it, don't.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.