Make Keyboard Disappear when UITableView Scrolls - ios

I have a UITextField at the top of my Table View, after the text field is selected I want the keyboard to disappear. I know to call [[self view] endEditing:YES]; but I don't know how to check for the scroll. A good example of this is IMessage, when the keyboard is in view you can scroll up to collapse it, I want my table view to work inversely.

UITableView is a subclass of UIScrollView. By being the table delegate (implementing <UITableViewDelegate>) you are also the scroll view delegate (<UIScrollViewDelegate>) and as such you can implement:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Then you will receive a notification whenever the table view is scrolled. You can then also use scrollView.contentOffset to check where the table has scrolled to (which direction it's scrolling in).

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[textField resignFirstResponder];
}
Try other method in UIScrollViewDelegate if you need other behaviour.

Call resignFirstResponder where appropriate.
It seems you use UITextField in your cells. Use UITextFieldDelegate protocol to know when UITextField ends being edited:
-(void)textFieldDidEndEditing:(UITextField*)textField
{
[textField resignFirstResponder];
}

Related

When UITextView is scrolling ...?

I have a textView which is not editable so my textView is only readable. And there is a ImageView on the top of textView. I want that, when the textView is scrolling, ImageView (Y position) disappear slowly depends on How many the textView scrolled.. If I should give an example, it looks like Twitter "Me" page but non blurred. I hope I was clear.
First of call UIScrollViewDelegate, make your viewcontroller delegate to self.Because it's subclass of UIScrollview.
and implement scrollView function according to your need.Suppose you want to disappear image after scrolling finished. then implement this method.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(#"Finished scrolling");
}

UITableView (inside UIScrollView) didSelectRowAtIndexPath: not being called on first tap

I have a non scrollable UITableView inside an UIScrollView. And I'm having the problem that when I touch a row, the callback didSelectRowAtIndexPath: is not being called on the first tap, but after the first tap everithing works.
A few considerations:
After the first tap, the table view works normally, every tap works in every cell.
This happens just after I scroll the UIScrollView. If I don't scroll the UIScrollView, this never happens.
I have overriden the UIScrollView's touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view and the event does pass throw here, the view is a UITableViewCellContentView effectively.
I just don't know why the event is not been sent to the UITableView on the first time, and on the following ones it does.
If you have UIScrollView that contains vertical content and UITableView as part of this content, you must at least disable scrolling on UITableView - otherwise it's confusing for the user when he will scroll your mainView and when tableView, and also confusing for the framework because it's not clear where to send panning gestures.
As a rule of thumb - you should avoid putting table views inside scrollViews, unless you really know what you're doing.
Please disable scrolling of your table view and check the datasource and delegate are connected to your table view. If not follow the bellow code
#interface myClass ()<UITableViewDataSource, UITableViewDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
self.myTableView.scrollEnabled = NO;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
}

UITableView method tableView:didEndDisplayingFooterView: isn't getting called

The following method won't get called when I scroll the tableView far enough so that it scrolls out of bounds.
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section
Why could this be happening?
Make sure that any UITextFields or UITextViews are not displaying their keyboards.
You can call:
[textView resignFirstResponder];
If any textField or textView is the firstResponder of the responder chain, the UITableView will not call the didEndDisplayingFooterView: method

UITextField resigns first responder when scrolling in UITableView isn't fast enough

I have a uitableview where each cell has a uitextfield. If i touch a text field low down in the table view, then it sets the text field as first responder and scrolls to it, and also scrolls up a keyboard (all as expected). But if the keyboard animation is faster than the table view scrolling, then the keyboard momentarily covers up the text field, which then therefore resigns as first responder.
It's hard to reproduce with a basic project or any code snippet that i could paste here, because the problem is that each cell is doing a lot of layout, and it's dequeueing other cells as it scrolls up etc, meaning that the scrolling is fairly slow.
I don't mind the slightly jerky scrolling, i'm just interested in finding a way of ensuring the text field keeps itself as first responder even when it momentarily is hidden by the keyboard.
edit:
as in the answer below, i'm now trying to maintain whether or not the table view is scrolling, and only allow resigning of first responder if scrolling = NO. The code for UITableViewDelegate is below, but the problem now is that non animated movements of the table view (i.e. scrollToRowAtIndexPath...animated:NO) result in scrollViewDidScroll, but not scrollViewDidEndScrollingAnimation. Any ideas on how to get round this in a way that isn't even more clunky?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"YES");
self.scrolling = YES;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(#"NO");
self.scrolling = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(#"NO");
self.scrolling = NO;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
NSLog(#"NO");
self.scrolling = NO;
}
You could subclass UITextField and implement
- (BOOL)resignFirstResponder
There you could return NO while scrolling.

Table view scrolling event

Is there any event in table view which will get triggered when i try to scroll the table view.
I want to create a down arrow image to the bottom of the table view ,when the user tries to scroll through the table view.Any help will be appreciated.
Thanks,
Christy
As far as i recall, since UITableView Inherits from UIScroll view you can listen to the UIScrollViewDelegate methods:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
{
}
Any way. make sure that
tableView.delegate=self;

Resources