Automatically scroll UITableView to show row **below** the row being edited - ios

I have UITableView in a ViewController (not a UITableViewController) with UITextFields in each row. I would like the TableView to automatically scroll so that the row below the row being edited is always displayed. This allows the user to tap the next row to edit it. The built in iOS Reminders app has this behaviour.
I have followed Apple's advice on managing the keyboard but that only scrolls to show the row you are editing just above the keyboard, not the row below it.
I have tried adjusting the frame in scrollRectToVisible: but it makes no difference. In fact commenting out that line of code seems to have no effect at all.
It seems that the UITableView will always try to scroll to show the UITextfield being edited just above the keyboard, and I can't find a way of adjusting or overriding this behaviour so that it shows the row below it also.
Update:
I've found that the automatic scrolling behaviour can be prevented by overriding the private scrollTextFieldToVisible method of UITextField.
However, the automatic scrolling code provided by Apple in the linked documentation still does not work.

You can try setting setContentOffset property instead of scrollRectToVisible. like
[self->_scrollView setContentOffset:CGPointMake(0, textFieldRect.origin.y)];

Please follow the below link, it will make your work very easy.
Just add the files and make the tableview class name as "TPKeyboardAvoidingTableView"
https://github.com/michaeltyson/TPKeyboardAvoiding

Related

How to avoid didSelectRowAtIndexPath being trigged when user tries to scroll bounce-disabled UITableView?

In my application, there's a View controller with bounce-disabled UITableView that covers the entire screen.
However, there is a situation where the table view contains only few items, which the sum of their heights is pretty equals to the screen's height.
As a result the table doesn't scroll (which is fine).
When the user tries to scroll in this situation, it triggers the didSelectRowAtIndexPath of the start scrolling point. How can I avoid this?
One easy solution is to allow bounce effect :D
Couldn't find any other solution :(
to make a better experience make the bounce situational
only allow tableView bounce if the (cellHeight * numberOfCells) < self.tableView.frame.size.height
Looks like some sort of bug in framework (excuse me if it is not). I got this issue today morning and now I am not getting it.
Try adding the below line of code to the cell.
cell.selectionStyle = UITableViewCellSelectionStyleNone
That is the only change I made to the tableview today, and not getting the issue any more. Reverting the change is not reintroducing the issue. Hence I am suspecting a framework issue.

UITableViewCell and the case of the missing Subtitle

I've checked all the usual suspects. I have a UITableView in an iOS 8 app that uses the UITableViewCellStyle.Subtitle and sets a subtitle for every cell. I won't bother including code as it's no different from any other code for this. The issue I'm having is best described as follows: my cells also have actions (the new API in iOS 8 lets you add actions to your cells accessible by swiping left on them, similar to Mail). Sometimes (seemingly random), however, the subtitles don't show up unless I swipe on them. Upon swiping, the subtitle appears. Scrolling the cell in and out of view again makes the subtitle disappear until I swipe on it once more. What could be causing this? The subtitle attribute is clearly being set on the cell, it just isn't visible when the cell becomes visible.
Additional info: when troubleshooting, I decided to disable the actions on the cell to see if that was the issue. However, the same problem would occur. Interestingly, with actions disabled, scrolling the cell out and back into view would cause the subtitle to appear.
I'm not unconvinced it's related to this: Subtitles of UITableViewCell won't update
You can chech my answer here for a temporary work around. Just put a blank space character in the interface builder inside the subtitle. check the answer for more details.

How to implement newsfeed comment page similar to Facebook or Instagram

Both Instagram and Facebook allow their users to comment on news feeds. In the comment scene, they basically have a UITableView with all the comments and a “footer” where user may enter comments and post them. So the “footer” is a UIView or UIToolBar with a UITextField/UITextView and a UIButton. Truly simple stuff from the look of it. Well I have been trying to implement it and the keyboard is not cooperating. I need the keyboard to not hide the “footer”. Especially now in iOS 8 the keyboard comes with a suggestions tool bar that a user may choose to show or hide. All these interactions make it difficult to keep my “footer” right above the keyboard while user is entering text. Every time I think I nail the solution, I find a multitude of bugs.
In one implementation, I use keyboard notification to listen for when the keyboard is up or going down (partly based on iPhone Keyboard Covers UITextField). The problem with this implementation is that there is a lag of about 1 to 2 seconds for when the keyboard shows up and for the “footer” to climb to the top of the keyboard from the bottom of the screen. A second issue is when user drags the suggestion toolbar to either show or hide it while typing, it causes my “footer” to move in unpredictable manners: which means I will need some static variables to meticulously track cumulative interactions with the suggestion toolbar just to fix that bug. Also notice that I put “footer” within quotation marks. That’s because I am not referring to a UITableView footer. But rather a view that I create beneath the table view as suggested by UITableView, make footer stay at bottom of screen?
Another implementation I tried was to use a “footer” and a keyboard ToolBar. When user clicks on the UITextField of the footer, that would cause the keyboard to show up with a replica of the footer as inputAccessoryView. This is basically to visually fool the user into thinking it’s the same footer that seamlessly climbs with the keyboard. But in reality I am using two compound Views: a “footer” and a keyboard toolbar. The first problem I encounter with this one is that I cannot seem to make the tool bar text field the first responder programmatically. This actually used to work in ios-7. But since I updated to iOS-8 it does not work. So if I do footerTextField.inputAccessoryView=keyboardToolBar and then in the textfield delegate method check if(textField==footerTextField){[tooBarTextField becomeFirstResponder];}, iOS-8 just ignores the whole thing or, worse, dismiss the keyboard and the toolbar altogether, so that in fact the keyboard never shows up when I click on footerTextField since the showing and dismissing happen so quickly.So again this used to work in iOS-7, but in iOS-8 it does not work.
a third approach was to make the parent view a TPKeyboardAvoidingScrollView such that I would have parent, and children UITableView and “footer”. Here while TPKeyboardAvoiding have worked for me on other scenes in the app, for whatever reason it’s not working here. My guess is because I am using a UITableView as one of the children of a UIScrollView.
a forth approach was to make my “footer” an actual UITableView section footer; section footer because I want it to float at the bottom. The problem with section footer is that they don’t stick to the bottom, which gives a visually erratic user experience as you scroll the table.
Ok, so I have said a lot. So finally, has anyone implemented a scene similar to Facebook’s/Instagram’s NewsFeed Comment scene? If so, will you please help me?
To recap: the Facebook/Instagram input textfield grows with text; sticks to the top of the keyboard whether the keyboard's suggestion toolbar is showing or hidden; sticks to the bottom of the screen when the keyboard is gone.
SlackTextViewController seems to fit all your requirements and is very well-written.

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.

iOS: Make content (text) in table view cell editable, but only in edit mode

I know this has been asked a lot of times but theres so much different information and different cases, I just don't understand where to begin and cannot get it working in my app.
In my app I have a table view and I'm using a NSFetchedResultsController. In normal mode, the table cells shouldn't be editable but instead each click should be a segue to a new viewcontroller (which works). In edit mode however I want that if you click on the cell, that you edit the text and this gets automatically stored to the database. And here I don't have a clue.
I want it as simple as possible. What I've read so far I guess I have to subclass UITableViewCell and a TextField. Here's already my first question: In each post about this, the TextField is init with a Rect, however how do I know the exact width and height of my cells?
And do I add this cell directly in the cellForRowAtIndexPath? Or only if the mode gets changed to edit? And some posts suggest you have to be the delegate for UITextField, but why?
And is there some sample code for about exact the problem I have? I wasn't able to find it...

Resources