views:

61

answers:

2

I have the following class structure for custom UITableViewCells:

NumericEntryCell - Contains method for controlling max value that can be entered into a textbox.

SliderCell - inherits from NEC and contains methods for handling slider-control of textbox value

TextOnlyCell - just contains a textbox

CellA - inherits from SliderCell and has a max value of 28

CellB - inherits from TextOnlyCell and has a max value of 150.

I want NumericEntryCell to contain a definition for a property that contains the maxValue. I want to be able to set a value for this in the initialize method of a child class (CellA or CellB).

Currently I have an int maxValue declared in the interface of NEC. I declare a property for this with @property int maxValue and @synthesize it within the .m file. However, attempting to modify this property's value from CellA or CellB's initialize method has no effect - when I hit a breakpoint in the NEC method that uses this value, its value is always zero. How can I get this to work?

+1  A: 

You say initialize, but probably meant init. The designated initializer for UITableViewCell is initWithStyle:reuseIdentifier:, which is where you might consider setting default values.

Edit (response to comments)

If I run a test, then +initialize is called when the class is set up (and only once). There is no call to any -initialize method, and there shouldn't be - so you are definitely doing something strange.

The problem with init methods is something different. Init methods are called by what I call "consensual programming", which means that it works if all the programmers involved do what they are supposed to, and that includes Apple programmers (and they get it wrong almost as often as we do).

Apple's standard template for a table view controller creates cells by calling initWithStyle:reuseIdentifier: - so if your code isn't doing this, you have a problem and need to handle init in some other way. At a guess, you have added a -initialize method, which is going to break consensus and confuse any other programmers you meet.

Your implementations also need to respect the calls to super that are expected in init methods.

Finally, having your cells laid out in the same nib as your table view doesn't make sense - they need to be loaded in multiply, once each time a new cell is requested, which means that they have to be in a separate nib (or created programatically).

Paul Lynch
I added a declaration for `-(void)initialize{}` and if I set a breakpoint here, it gets hit when the cell is instantiated.
Daniel I-S
`initialize` is called the first time a class is ever used, before `alloc`, but it's never called again. `init` is where you actually initialize the class.
eman
Right, I thought that might turn out to be true. The problem I now have is that, for my class, `initWithStyle` never seems to get hit. The way I have the cells being displayed is as follows: they are in the same nib file as the listview, and are assigned to the referencing outlets of a series of properties in my ListViewController class. The relevant property is returned to the ListViewController in its `cellForRowAtIndexPath:` method. Which init method would correspond to this method of instantiation?
Daniel I-S
Responded by editing the answer.
Paul Lynch
Thanks; there's some useful information there, although JeremyP provided a direct solution to my problem. In regards to having cells laid out in the same nib as my table view, however - each cell is only going to be used once, and is referenced directly in an IBOutlet of the table view controller, so when I provide a cell to `cellForRowAtIndexPath:` I am providing it with an already existing instance directly, by name. What kind of alternatives do I have to this when I need to access controls in these cells (from the view controller) later on?
Daniel I-S
With singleton cells, that's ok. When you load from a nib, you can use tricks like [cell viewWithTag:n]; - you have to set the tags in the nib.
Paul Lynch
+1  A: 

Having read the comments to Paul's answer, you might want to consider initialising your maxValue variable in -awakeFromNib. Objects in a nib are effectively already initialised and might be instantiated in the application using initWithCoder, not the designated initialiser.

JeremyP
Thanks; this worked perfectly.
Daniel I-S