UIPickerView in UITableViewCell - how to select values instead of scrolling TableView? - ios

I've got a custom UITableViewCell that has a label and a UIPickerView. Display works fine, but when I want to select a value in the Picker, the TableView scrolls. What can I do so that the gestures in the cell go to the Picker instead of the whole TableView?
The only solution I could come up with was to set the whole TableView to scrollEnabled = NO. This works for the Picker, but now I can't get to the cells under the custom cell. Control has to be more fine-grained.

If you can get hold of the UIGestureRecognizer for each of the two gestures, and tell one it needs to wait for the other to fail. With one in every cell, that becomes over-wieldy.
Perhaps you should add a control or have the accessory view "bring up" the picker, and then dismiss it when done.

Related

Reload table view on editing UITextView inside UITableViewCells

I have some customised UITableViewCells which contain a UITextView and a set of buttons. The buttons in the cell should be visible only when the user tries to edit the text view in the corresponding cell.
So, if the user attempts to edit the textview in cell1, then the set of buttons should be visible below the textview in cell1 and the height of cell1 should also be increased. Now, if the user attempts to edit the textview in cell2, then the set of buttons should be visible below the textview in cell2 and the height of cell2 should also be increased, whereas the buttons in cell1 should get removed and cell size needs to be calculated accordingly.
For this I tried to reload the table view cells from textViewDidBeginEditing:. This is reloading the cells properly and shows/hides the buttons properly in the required cells, but does not allow proper editing of text view. When the user tries to edit with the textview, the tableview reload methods are invoked constantly and not allowing the keyboard to stand for editing.
Is it right to handle reloading from textViewDidBeginEditing: in first place ? is there some better way to do this ? please help.
First I would suggest that do not reload the entire TableView each time. Instead use the
reloadRowsAtIndexPaths
method to load only the cell in which you want modifications.
Next, to solve your issue regarding the textView, you could do something like this, declare a class property of bool type, and set it to false. When you reload your cell for the first time, set it to true. Now in the textViewDidBegin editing method, check for this bool. If it is set to true, that means you already have loaded the cell and you do not need to load it again, so in this case do not call the reloadRows method. Else if it is false, reload the rows and set this bool to true.
Now in the textViewDidEndEditing delegate method, set this bool to false again so that when a user taps on another textView in some other row, it is reloaded properly.
This logic may not be perfect, you may require some tweaking. But it will get the job done
Explicitly make the textfield as firstResponder
if buttonsDisplayed == NO {
reload cell
}
if textFieldIsFirstResponder == NO {
[textField becomeFirstResponder];
}

Updating label in a cell of UITableView

My UITableViewCell has a cell template which is created from another nib file. The cell has a UIlabel object. Now, once the UITableView has loaded and the text has been displayed, and if I want to change its value by clicking a button from another cell, How should I do it ?
I have updated the text of the UIlabel but how to show it on the screen? Should I reload the entire table? Kindly let me know if there is any good way to do it.
You can use KVO for this purpose. Each cell observes the model, and when it changes, update some fields.

Keep UIKeyboard up while reloading UITableView section

I would like to know if there is a way to keep UIKeyboard up while reloading section in UITableView? I have UITextField inside a header view of UITableView's section. Typing into this UITextField fires action that requires a section to be updated (reloaded).
As a result of calling [tableView reloadSections:...] the keyboard hides itself, because UITextField loses it's firstResponder status.
I would like to achieve similar effect like when using UISearchBar component in UITableView.
Thanks!
If you reload, everything will get refreshed. When that happens, the current first responder is resigned and the keyboard is animated out. To avoid that you need to no reload...
You would need to update the visible cells directly and use insertRowsAtIndexPaths:withRowAnimation: and deleteRowsAtIndexPaths:withRowAnimation: to make changes to the number of rows the table is managing. In this way the section won't be reloaded and you will avoid any cell animations / refreshing of views.

Dealing with keyboard and tableviewcell

The layout for one of my View Controllers is such: I have a scroll view embedded inside my VC. Inside my scroll view, I have a table view that consists of 5 cell. The first 3 cells consist of a textfield thats pulls its text from a dictionary and this changes depending on certain situations. These textfields are editable and so tapping on them brings up the keyboard, the issue however is that I would like my view to scroll when I tap on the text field because right now they keyboard hides the the third editable text field. Another issue is that at the moment, clicking outside teh table view doesnt cause the keyboard to be dismissed and so the only way of dismissing the keyboard is tapping on the return key. What I would like to happen is that when I tap on either one of the 3 editable fields, the scroll view ought to scroll up a certain number that I define (this is so that I can define how much to scroll depending on which row is currently selected). One of the issues I'm facing is that I can't directly reference these textfields in my VC since they're all created through one prototype cell. My thinking was that I could create a dictionary with the 3 textfields as keys and then the scrollview y coordinates as values and then use that. However , I wasn't sure how to do this in my situation (with the prototype cells). Would really appreciate if someone could show me some sample code on how to do this.
You can reference your text fields by calling cellForRowAtIndexPath: to get the UITableViewCell, then calling viewWithTag: to get your UITextField. Just assign the text fields a tag number. Also, set the text field's delegate to be your view controller so that you can respond to a user tapping to edit text.

Make UITextField inside a table view visible scrolling

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.

Resources