how to show the UItableViewcell where the user is writing? - iphone

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?

Related

Textfields don't get selected after reloadData on UITableView

In my UITableView I have rows which contain 4 textfields each. After user ends editing couple of things in UITableView datasource are being recalculated, so I need to reload UITableView after edition of textfield. But because tableview is being reloaded, when user touches another textfield to edit while editing the first one, second textfield doesn't get selected. He has to touch it again before it can be edited.
Is there a way to avoid this behaviour? Unfortunately I do need to use reloadData instead of reloadSections or reloadRows methods. Any help will be appreciated, thanks!
Reloading a table view will resign the first responder of the text field or even text view. You could try this trick if you have multiple changes that need to be made.
[[self tableView] beginUpdates]
[[self tableView] endUpdates]
Try assigning a gestureRecognizer to each field or same one to all fields. Once you capture the touch in required field, capture the tag and make that field the firstResponder.
Summing up all the help I received and my own ideas I've gone with the following:
-Delay reload for 0.01 sec so textFieldDidBeginEditing gets called
-Then save textfield's tag and cell's index path to variables in textFieldDidBeginEditing
-Call reloadData on tableview
-Call becomeFirstResponder on textfield saved with variables
This however caused keyboard hiding for a brief moment as it didn't have any responder during reloading. To resolve that I created a fake textfield in tableview's superview and assigned him as a responder up to a moment when tableview was reloaded to assign back to the textfield that was selected by a user.
Certainly not the cleanest solution, but it works.

iPhone - changing contents of a UITableViewCell inside a navigationController

I have a UITableView with some cells in it that have people's names. When the user clicks on a cell, the Navigation Controller pushes a new view onto the stack that shows detailed info for that person. When the user is done and clicks the button to return to the previous view, the tableview is again shown. The problem is that I want to edit the cell text (or just mark the cell in some way) to show that the user has clicked on that cell. In my cellForRowAtIndexPath: method I have set up code that should change the text of the cell after a user clicks on that cell. I set up break points and it IS entering cellForRowAtIndexPath: when the detail view is popped and the tableview is again shown. It's entering the segment of code in cellForRowAtIndexPath: that should change the text, but when it comes up on screen, the text has not been changed. Does anyone have any ideas as to what is going on? Thanks.
EDIT: I figured it out. I just needed to call [tableView reloadData]; instead of calling the cellForRowAtIndexPath method. Not sure why that works differently, but it fixed it.
I guess I'll help you out. You don't need to credit me with the answer though.
cellForRowAtIndexPath:
that method is called within the framework when a cell is being refreshed from the queue or when it needs to be created. You should not call this method.
[tableView reloadData];
that method is basically a refresh on all of the visible cells show in the UITableView. This is the method you should call if you change information in your data source or you need to (force) update the appearance of your cells.
It's Good You Have Reload Table And Changed The Text But If YouJustIndicate NAd Don't Want To Change The Text The nYou Can Use elow Given Text ....
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
It Will Check Mark Particuler Cell Which You Click/Visited......
Hope You Like This Solution.....

iPhone didSelectRowAtIndexPath not firing

So I am trying to default to having the first row in a tableView selected when my view first loads. I am doing this my viewWillAppear method:
NSIndexPath *tempPath= [NSIndexPath indexPathForRow:0 inSection:0];
[theTable.delegate tableView:theTable didSelectRowAtIndexPath:tempPath];
Which fires the method, but the cell doesn't stay selected. This fires the proper method and executes the code in there, and I am never deselecting it. After the user presses another cell, it stays selected properly. Just not the first time. Does anyone know what may be causing this weirdness? I have even tried to do this:
[[theTable.delegate tableView:theTable cellForRowAtIndexPath:tempPath] setSelected:YES];
Thoughts?
First of all, you shouldn't be calling the delegate methods directly like that.
If you do need to select a cell programmatically (like the Mail app does), use the selectRowAtIndexPath method:
[theTable selectRowAtIndexPath:tempPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
According to the HIG, you aren't supposed to leave table view cells in a selected state. You are encouraged (strongly) to deselect the row as soon as you perform whatever action the tap requires.
For persistent selections, you are supposed to use the accessory, and you can set a cell's accessory to a checkmark to indicate "Selected"

iPhone next UITextField

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.

Redoing UITableView layout when user taps Edit button?

I have a UITableView with complex content. The user can edit (rearrange and delete) the cells when tapping the Edit button the standard way. But I want the cells to look different in "edit" mode.
Question:
How to change the UITableView Layout in edit mode, including changing row height?
So far, this is what I have:
The Edit button sends a WillTransitionToState/DidTransitionToState message to each uitableviewcell (UITVC). I have subclassed UITVC and react to these inside each cell, hiding and removing and reshuffling as needed. But, changing the row height is beyond the scope of one cell.
There does not seem to be a message sent to UITableView when user taps edit. There is a - tableView:commitEditingStyle:forRowAtIndexPath: sent to data source after editing a particular row.
Inside heightForRowAtIndexPath, I can query the current mode using the tableView.editing property, and report height as appropriate. And I can trigger re-flowing the table, including recomputing the heights, by invoking [tableView reloadData]. But, when do I call it?
I could send messages from the cells from within WillTransitionToState back to the "owning" table view, and call reloadData when I get them. But this sounds fragile and there must be a better way.
Rhythmic is right. Using reloadData kills the nice editing animation.
This problem is addressed in this post:
Can you animate a height change on a UITableViewCell when selected?
Instead of using reloadData, do the following after calling setEditing:animated.
[tableview setEditing:editing animated:YES];
[tableview beginUpdates];
[tableview endUpdates];
If you wish for your table cells to change their format in response to whether or not the table is in editing mode, you could override -setEditing:animated: in your UITableViewController and trigger a reload (via -reloadData) of the table view on a change of editing state.
Within your UITableViewController's -tableView:cellForRowAtIndexPath: method, you could check for whether or not the table was in the editing state by querying the editing property on the table view, and then return a different cell type depending on which state the table is in.