A Question about Assign Properties
Edit 2: What I previously planned was probably a bad idea and I now changed my design: My UITableViewController
has an array with all the values of my UITextField
s and I am using delegation to update the values in the array. (If a value in one UITableViewCell
changes, I send a message with the new value and the index of the cell).
Original Question
I would like to create a UITableViewCell
subclass. To access my cells, I would like to have an NSMutableArray
in my UITableViewController
with all the cells. Whenever I create a new cell in - 开发者_开发技巧tableView:cellForRowAtIndexPath:
I would add it to the array. The cells should however know about this array. I would declare a property like this for the UITableViewCell
:
@property (nonatomic, assign) NSMutableArray *cellsArray;
Whenever I create a new cell, I would set its cellsArray
to my array.
My (probably simple) question is: Is it correct that cellsArray
will hold a pointer to the array in the UITableViewController
and when I add stuff to the array of the UITableViewController
, the cells will know this too, i.e. can access it?
Edit: The UITableViewCell
s contain UITextField
s. I used to rely on the -cellForRowAtIndexPath:
method and the visibleCells
array, however when the cells moved out of view, the content of their UITextFields
would also be lost. I then decided to store the cells in an array. When the user taps save, I iterate through the array and store the values. Also, I would like to automatically update the enabled
property of the save button, depending on whether all cells contain something - for this I need all cells, too.
The cells should know about the other cells so that they can select the next cell when the return/next key on the keyboard is pressed.
If there are better approaches to this, I am glad to hear about them!
Not a direct answer of your question, but this sounds like a very bad design. Why should one cell need to know about its siblings? Any event/change that occurs in one cell and has an effect on the other cells should be handled by the table view controller. The single cells should be separate entities that should have no need to know about the state of each other.
Secondly, there is no need to introduce another array to manage the table cells. The table view already has a property visibleCells
that you can access from the table view controller. And should never have to interact with invisible cells anyway because those are managed by the table view and its reuse facility.
I believe the answer is Yes.
My understanding of assign
is that you can assign a value to such a variable and the retain count for the original object is not incremented. Similarly you need not release the variable in the object's dealloc
method. You may run the risk, however, that the original array goes away and then cellsArray
is pointing at something that is no longer there.
I use assign
when I want to pass a reference to an object to another object (e.g. a view controller that is going to display or otherwise manipulate the object). And in the latter object, I do not release it's pointer to the object.
You also see assign
used with properties that are id
's, like
@property (nonatomic, assign) id<SomeProtocol> _myDelegate;
All that being said, with the exception of the id
case, often I feel "safer" using retain
for the property and being sure to release in dealloc
. :-)
Anyway, I think that's the crux of the difference.
精彩评论