I have an application with a UITableView with grouped style. On each of the cells, there is a UILabel with a description, and a UITextField for the user to enter data.
We implemented a toolbar on top of the keyboard, with next and prev buttons (similar to what MobileSafari provides when filling forms). In order to give focus to the next field, we resorted to using the tag attribute with consecutive numbers, and we find the subview by tag.
We can't use the responder chain because all the textfields have a different parent view (their UITableViewCell).
However, when using the iPhone Simulator, the Tab key on the keyboard (and Shift-Tab) work as expected and will move the focus to the next or prev textfield, regardless on where it is in the view hierarchy. After seeing that, we are sure there must be an API that provides us the required functionality, but we could not find it. Any hints?
I'm not sure exactly what functionality you are trying to implement.
However, I suspect it will be of interest to you that you can access the controller from the cells as follows:
UITableView *myTableView = (UITableView *)self.superview;
NSIndexPath *indexPath = [myTableView indexPathForCell: self];
MyTableViewController *myTableViewController = (MyTableViewController *)(myTableView.delegate);
Once you have messaged the controller, it in turn can access whatever cell is desired with cellForRowAtIndexPath.
Related
I know this question has been asked before but I couldn't find an answer that applied to my problem.
I've got a UITableViewController that has a third row that is filled with a UITextView.
I'm quite happy with the way it looks and the way text is typed into it.
However I'm unable to find a way of getting rid of the keyboard once the user is done entering text. I'd like to be able to use the return button for actual \n in the text.
I've gotten this far that pressing the upper two rows will make the textView te resignFirstTransponder but is there a way to catch a tap on the greyish background?
This is all in a UITableViewController loaded from a nib file.
Btw, I'm quite new to iOS programming so the more elaborate your answer the better :)
Thanks!
A pattern many apps follow is to show a horizontal bar with buttons on it just above the keyboard. It can contain a done button clicking on which you can hide the keyboard. And of course you will have to create that horizontal view yourself.
Another way would be to enable a touch recognizer elsewhere, and on a tap outside hide the keyboard
One alternative would be to add a toolbar to the keyboard with something like a "done" button that will dismiss it. You can find some sample code about that here. One second approach would be to dismiss the keyboard when the user selects a different cell or even when the tableView scrolls. In order to do that, you can add relevant code in -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath or in -(void)scrollViewDidScroll:(UIScrollView *)scrollView respectively.
This can get a little tricky if your new at iOS. The way I handle a UITextView in a UITableViewCell is I make a custom UITableViewCell subclass with an outlet for the UITextView.
Then set your cell in interface builder to be of that subclass. In the CellForRowAtIndexPath:
set the delegate to self. Then in the DidSelectRowAtIndexPath you call the delegate for the TextView based on the indexPath, this way the keyBoard will dismiss for the correct row if you touch the background. If you want it to dismiss when the user touches any cell just call the delegate without specifying the indexPath. The Delegate is TextViewShouldEndEditing:.Ill post some code if you want.
I Have an UITableView with textfields in each row.
I want to access these textfields each in a different way like adding a picker view for one textfield, navigating to another view when clicked on one text field.
i know how to do it when these textfields are in a normalview but i never have done it before on tableview.
Please help.
Your question is where to put the code to handle user interaction with your table view cells. Well the proper place to do that is the didSelectRowAtIndexPath: method of your table view controller.
However, there are a few caveats.
You need to add the UITextFieldDelegate protocol to your table view controller.
You can distinguish between the active UITextFields with tags, as suggested in other answers.
However, recycling the cells can lead to big confusion about the text fields. So make sure you update that id tag in the part of cellForRowAtIndexPath: that is not creating new cells.
You should not use a UITextField when you want another controller to pop up. In this case, just use a UILabel or the provided textLabel property of UITableViewCell. Push another controller when the user selects the row.
Only use a UITextField if you want the keyboard to pop up without leaving the table view.
And one more thing while assigning tag to text field, don't forgot to this line
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil] autorelease];
i think you need to give tag to each textfield like
textfield.tag = indexpath.row;
according to tag you can move ahead...
I have a tableView with a lot of field, like a form. The question is that when the user is changing to the next row, if the row is bellow the keyboard, the user does not see the label nor the text his writing.
I have try to use:
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[self.theTable scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
but without success.
Let me tell you that the viewController is controlling a view, with a table inside it. The table is not the view itself.
How can I make the cell becomes visible, so that the user can see, even if that's the last row of the table (has about 20 rows)?
Either use a subclass of UITableViewController (which already handles this automatically) or resize your table view when the keyboard becomes visible, so that it's not behind the keyboard. You can also use the contentInset property instead of changing the table view's frame.
To be notified when the keyboard shows or hides, listen for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification.
For handling the keyboard yourself look at this answer resize UIView when showing the keyboard for iphone, how to?
The Facebook app for the iPhone has a great username/password setup where it almost looks like it is a uitableview with UITextField's inside the two rows, is this how it is done? Or would it be something different to this altogether?
If it is a tableview, how can I access the textfields inside the tableview once the user hits the Login button? Is there a quick way to iterate over the cells in a tableview?
A simple one, I know, but I didnt want to implement an elaborate tableview if I didnt need to...
The source: https://github.com/c99koder/lastfm-iphone
Edited: It's possible you customize the UITableViewCell and put a UITextField inside. However, there is an even easy way to achieve this, since the login / signup page would almost always be static. See below.
LastFM for iPhone used a similar login / signup page design. And they open-sourced their code. Take a look at the nib file and you will know how LastFM did this. :-)
Screenshot 1: original
Screenshot 2: after I removed some of the background png files.
Create a tableview controller and go to the xib file -> attributes inspector->tableview and select "group styled" instead of plain. Then u can customize the tableview cells in the code by adding the textfields for username and password. Then create a new UIView and set the view to the tableview's footer view for the login button.
For more explanation regarding the same, go through the answer for this question.
Table view in iPhone in grouped style
It looks like it's not using a UITableView at all (otherwise, you'd be able to scroll the page up and down). It's just two UITextFields over custom backgrounds (or with custom backgrounds).
Now, if you wanted to use a UITableView, what you could do is store as variables the UITextFields within the UITableViewCells. Then, when the person clicks a login button, you just get the text value from the UITextFields.
But, again, if you wanted to emulate the Facebook look, you don't need to bother with the UITableView.
Hope this helps!
Alright, now that I understand your question (thanks #Jonathan!), yes it does look like it is a tableview using custom UITableViewCells with a UITextField.
After creating the custom cell with a UITextField you can assign your view controller as the UITextField delegate and access the values at various points through the delegate methods:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
As an example, in your textFieldShouldReturn:, you can grab a reference to the indexPath of that cell with :
NSIndexPath *indexPath = [self.loginTable indexPathForCell:(MemberLoginCell*)[[textField superview] superview]];
And then to the cell with:
MemberLoginCell *cell = (MemberLoginCell*)[self.loginTable cellForRowAtIndexPath:indexPath];
Or simply grab the textField.text value and assign it to an NSString that can be passed to your LOGIN method as required.
I know there's already an accepted answer, but I created a facebook-type login page, using a UITableView. The code is here on github
I am looking for the most direct way to make it so that when the user taps a UITableViewCell, a keyboard appears. I want to use a custom keyboard (UIPickerView) and I preferably would like to make the cell style UITableViewCellStyleValue2. I can't seem to find a very direct way of doing this. I have a navigation bar on top, and hoping to make the buttons on that change as well...
Thanks!
First and foremost, to achieve this you're going to have to handle the custom animation of the UIPickerView sliding up and down. They keyboard is handled automatically by the controls that automatically need it (UITextField, UITextView, etc.).
So when your view loads you will want to create and configure your picker and then move its Y coordinate to CGRectGetMaxY([[UIScreen mainScreen] applicationFrame]);
Then in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you will call a method that is responsible for animating your picker view into place. The only problem I foresee with this approach is allowing the user to dismiss this picker view in a way that makes sense as users are used to input views sliding only when they're needed (meaning that they appear when a view becomesFirstResponder and they disappear automatically when that view resignsFirstResponder status).
I think I understand what you're trying to achieve here and I would do it a bit differently. Instead of displaying a picker with options when you select a cell I would instead push a new tableViewController with your options laid out as cells. Then when the user makes a selection, you can set a checkmark and pop back to the original view.