Dealing with keyboard and tableviewcell - ios

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.

Related

Accessibility focus goes in space in between the cells in a table view

I am using a table view to show my data. There is no separator view in between the cells but when I switch on the voice over, the focus after on the cell, goes in the space between the two cells ans then on another swipe goes to the next cell.
I am not able to figure out what's going wrong.
The tableView is being imported from another framework, where it is working fine.
The separatorStyle for tableView is set to none.
I just figured out the problem.
In the framework where tableView is populated, array.filter{ !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } was missing at one place where the tableView was passed with the array as data.
so, that focus was on cells with empty spaces as data.
Thank you

keyboard hides text boxes in static table view

Object C - iOS development. Hello all. I have a static table view with several text boxes to enter information. When I tap on a lower box, the keyboard hides the text box preventing entry of text. This app uses another static table view controller and when a lower text box is tapped, the view scrolls up allowing text to be entered, so it works fine. I must be missing a setting of some sort, but can't find it. Your help is greatly appreciated.
If you have a full screen table view controller by a subclass of UITableViewController then the keyboard interaction will be handled for you.
If you don't (you aren't using UITableViewController) then you need to do it yourself. Basically, you need to observe the keyboard notifications and use the keyboard frame they provide to you to change the table view frame so that it isn't below the keyboard.
See the docs for UIKeyboardDidShowNotification here. You might also like this project.

Cocoa - How to make a search field for a tableView?

I have a table with several results, and now I want to make a customized search field (just a box with a text label). And I want that the table results instantly match what the user types on the search field.
Example: I tap "AN" and the table results are "Ana Fernandes", "Ana Alicia", "Ananás", etc.
Can you help me with that?
You will have a UIViewController with a normal view (ie self.view). Add a UISearhBar as a subview of self.view and put its origin at 0,0. Add a UITableView as a second subview, and set it's origin to (0, searchBar.frame.size.height). Now you have both views you want. Add yourself as a delegate to the searchbar, and implement the delegate methods so you can see what the search string is as the user enters text. You will then fuss with your tableview to just show the cells you want.

edit the text of a cell?

I have a UITableView, and want to let users edit the text of a cell. I want to popup a new UIView to let them enter some text in textfield, then dismiss the view and replace the cell's label text. Is it possible?
This is kind of an advanced move, Vikas. The programming of and user interaction with table view cells is tricky. The reason is that the UI code is a bit tricky because of the scrollable view, and you have to be prepared for the table cells to move or change if the table data is reloaded.
Is there another way you can achieve the result you're looking for? Could you design your table view so that when a cell is selected, you push a new view onto the navigation stack, and have UI components on that view that enable the user to edit the value?
This is the way that Apple does the Settings on iOS. When you select a row, you are shown a new view that has appropriate editing or selection controls.
You should use the textLabel property.

How to show keyboard programmatically

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.

Resources