UITableView dynamic cells with a UITextField GCed? - ios

I have a UITableView in which I create my own Cells by adding UIViews to the cell based on the index which is needed as I have about 15 very different cells in one table.
This all works fine, except when I have a UITextField; when I click on the text field, the application crashes which, what looks like something has been garbage collected and now it's still being used.
To reproduce , you simple make a UITableView with a cellForRowAtIndexPath which returns a Cell view to which you add, in the cellForRowAtIndexPath method a UITextField. When the UITableView appears you will see the text fields (and anything else you might have added to it), but clicking on the text field will crash when it becomes first responder.
I'm probably missing something trivial where it is logical somehow but I cannot find references to it from others (or i'm not searching correctly of course).

Related

UITableView trigger when cell is reused

I'm having some issues with reusable cells in a UITableView. I have several types of cells, that I declare in the constructor.
My issue is that I have one particular type of cell that contains a UITextView and I have an issue when I scroll the table, the text within is lost. I need to save this text to the models that accompany the cells and then put the text back when the cell is used again.
How do I know that the cell is being moved away from? I have other types of cells, so I need a way to invoke some code to do the saving part on the scroll of the UITableView.
I hope that makes sense, if more is required, let me know.
Thanks.
Just save the text once it is changed to the model, check if any text is present and use that in tableView(_:cellForRowAt:)
For more help you will have to show us your code.
You can inherit UITextViewDelegate and in the textViewDidEndEditing(_:) check if the text view is edited, then you will be able to store the text in a variable or somewhere else and restore it whenever you are about to show that cell again.
If there is more than one text view, you might want to set an accessibility identifier for each kind, so you will find out which one did end editing.

Getting the data of all TableView Cell which all has TextFields in it (including the hidden views)

I'd like to get every data that is within all cells in one tableview which is quite a long list.
I'm looking for an approach on how to retrieve everything including those hidden in view, which I know the views are reused. I think some of you might have experienced this problem before, what are your approach on this?
I've tried
let cells = self.tableView.visibleCells
then looping into every cell and saving each data to an array but it is not effective in getting those that aren't part of the view or hidden. Is there a way to get over this?
In cellForRowAtIndexPath, YOU are telling the table what is in each cell. So why would you turn around and ask the table what's in each cell? If the user puts "Hello" in your first cell, then scrolls the table enough to push that first cell out of view, then when the user scrolls back to the top, YOU are the one telling it to put "Hello" back in that first cell. YOU own the data source, not the table.
You need a data source. That can be "empty" at first, maybe an array of empty strings if that's what you want (each index in the array could map to a table row for example). But then, as the user interacts with the text fields in the cells, you need to update that data source with the text they entered.
You should use that data source as your source for the cellForRowAtIndex method. That way you can handle populating the cells when they are requested by the table, and you also know all the data when the user is done.
Why not just update the model each time the user taps a key when editing a textfield? You could create a protocol for that cell subclass and make your view controller the delegate for each cell. As long as cells are guaranteed to stay on the screen while you're typing (you'll get some weird behaviors if not) the cell can send a message to the view controller or whatever you hook it up to telling it what new value to store. Then everything is already stored for you when you need the full list, and you don't have to interact with the tableview.

how to get all text from uitextfield in uitableview

i have a table view with a UITextField in each cell.the table has 30 lines。
i want to get all values from 30 UITextfield,But it works fine for 10 visible cells. I cant't access any invisible cells. the text returns null. please help me.I am a chese developer
I cant't access any invisible cells. the text returns null.
That's how it is supposed to work since UITableView optimizes its use of cells and only displayed cells are actually existing at any time. (at least, if you are using dequeueReusableCellWithIdentifier: as you will find in most UITableView tutorials).
You should possibly store the text entered by the user in each table row somewhere (in your model?) and then access it from there.
Alternatively, you could avoid using dequeueReusableCellWithIdentifier: but in that case you should manage the set of cells in the table view on your own. (I am not encouraging your to do this, it's just a possibility).

iOS: Make content (text) in table view cell editable, but only in edit mode

I know this has been asked a lot of times but theres so much different information and different cases, I just don't understand where to begin and cannot get it working in my app.
In my app I have a table view and I'm using a NSFetchedResultsController. In normal mode, the table cells shouldn't be editable but instead each click should be a segue to a new viewcontroller (which works). In edit mode however I want that if you click on the cell, that you edit the text and this gets automatically stored to the database. And here I don't have a clue.
I want it as simple as possible. What I've read so far I guess I have to subclass UITableViewCell and a TextField. Here's already my first question: In each post about this, the TextField is init with a Rect, however how do I know the exact width and height of my cells?
And do I add this cell directly in the cellForRowAtIndexPath? Or only if the mode gets changed to edit? And some posts suggest you have to be the delegate for UITextField, but why?
And is there some sample code for about exact the problem I have? I wasn't able to find it...

Does a UITextField with first responder status in a UITableViewCell prevent cell re-use?

In a UITableView where the cells contain UITextField objects, does having one of those UITextField objects be the first responder prevent its cell from being re-used?
For example, with a UITableView that has more rows than will fit on the screen, one can tap on a UITextField to bring up the keyboard for the field in that cell. Then scroll the table view to make that cell go off screen. When scrolling back to the cell, I can see calls to -tableView:cellForRowAtIndexPath: for all the cells, except the one that contains the UITextField that currently has first responder status.
This behavior leads me to believe that iOS is aware that my cell contains the first responder and thus chooses not to discard the cell, thus not needing to call the data source to get the cell when it scrolls back into view.
This behavior is desirable, but I am concerned because I have not seen any documentation that indicates that this behavior is guaranteed, and I would hate to rely on it if there are conditions where it isn't true.
Can anyone point me at some documentation about table view cell re-use and first responders that covers this situation?
There is no such reference in the official iOS SDK documentation. You are correct to be concerned that this behavior you are observing is not guaranteed and should not be relied upon.

Resources