Bear with me as this is a difficult issue to describe. I have two UICollectionViews: MainCollectionView and NestedCollectionView. The MainCollectionViewCell holds a UILabel and the NestedCollectionView. In turn, the NestedCollectionViewCell holds either a UILabel or a UITextfield. Whether the UILabel or UITextfield is shown depends on the content for the NestedCollectionViewCell.
My issue is as follows: when the user selects the NestedCollectionViewCell and the content is set to show a UITextfield, then a keyboard for user input shows up. This is expected.
However, when the user inputs some text then scrolls to another cell in the MainCollectionView, the NestedCollectionView gets reset and the UITextfield.text is set to the default. In other words, the inputted text is not preserved.
Any advice how I can solve this?
Your inputted text in UITextField is lost, because whenever you scroll your UITableView it will automatically destroy cells which are no longer visible, and this also required for better memory management otherwise your app will consume more memory. So when you come back to your previous cell, you have newly generated UITalbeViewCell. So your data is lost, because you haven't persist it.
So as a solution whenever your textFieldDidEndEditing called or whenever you dismiss keyboard store your UITextField values into some object, and in your cellForRowAtIndexPath method check if object has value then fill it to your UITextField.
In this way you always have your previously entered value as it is.
Related
I have a TableView with some Cell's that contain a UITextField and some with a UITextView.
While presenting a UITableViewController i'm getting a strange behavior. The TextView has a property called "Selectable":
If I leave this checked, the last Cell that contains a TextView becomes automatically focused. And Of Course the keyboard will popup and hide content.
If I don't check it, it won't become focused when the view loads - that's what I want - but no touches in the Cell's TextView are not recognized.
So any idea why:
The Selectable property prevents any touches?
Whats the difference then between Selectable & Editable?
Any Solutions for my dilemma?
selectable means that data detectors (dates, addresses, etc., in accordance with the dataDetectorTypes property setting) act as links.
editable means that the user can type, select, copy, and paste the text itself.
Also, don't trust the Interface Builder (nib editor) interface on these properties. Set them in code.
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).
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.
I have a UITableViewController, a bunch of sections and rows, and for each row I added a UITextField as a subview, right aligned in the row itself.
If the users taps on the row, I locally save the indexPath, make the corresponding text field become the first responder and finally, when the keyboard appears, I make the table view scroll so that the row remains visible.
I am facing the problem to obtain the same behaviour when the user taps the text field instead. In this case the didSelectRowAtIndexPath: method isn't called, so I do not know how to tell the table view to scroll to make sure that the "selected" row is still visible.
Probably the whole process is not correct. Do you know a way to solve it?
Thanks a lot!
I'm not absolutely sure about this, but...
Set the userInteractionEnabled property of the UITextField to NO. This way, the touch goes "through" the control, tapping the UITableViewCell. When didSelectRowAtIndexPath: is called, set the userInteractionEnabled property of the UITextField to YES. When the editing is complete, change it back to NO.
I have UITableView where each cell consists of two UILabel, I want to show up keyboard when the cell is selected? Is it possible with UILabels?
If you just want to pop up a keyboard, you can add a tiny invisible (transparent 1x1 with transparent text) UITextField anywhere in any visible view and make this text field first responder to pop up a keyboard. Then you can redirect the input text to any of the two labels (or somewhere else) using the text field delegates to capture the input.
Yes, the label has to conform to the UIKeyInput protocol. Note that this is an either-or proposition. If the label conforms to UIKeyInput, then when it becomes first responder, the keyboard will be displayed, whether you want it or not.
I'm not sure how you mean this exactly since it is obviously not possible to edit two textfields for labels at the same time. Hence the following assumes you want to show the text in your cell using UILabel, but want to be able to edit the cell's text.
You can't directly use the keyboard to edit UILabels. The easiest solution is to directly use UITextFields instead of the UILabels.
An alternative is to have both a UITextField and UILabel in the cell. Then show the textfield (by settings itsß hidden property toYES`) when the cell is selected and hide the label. When editing is finished, do the reverse (i.e. showing labels, hiding textfields).
To show the keyboard directly after selecting the cell you can call [someTextField becomeFirstResponder];. To check if the user is done editing (and e.g. tapped the return key), you can set the delegate of the UITextField.