UITableView scrollView delegate not called - ios

I have a tableview set inside a UIViewController
#property (strong, nonatomic) IBOutlet UITableView *tableView;
with delegates set in viewDidLoad
self.tableView.delegate = self;
self.tableView.dataSource = self;
The following methods never get called:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
Isn't tableviewdelegate supposed to conform to uiscrollviewdelegate as well?
Any idea why these methods are never called?

You need to do two things:
1) Set the UITableView's delegate property, as was suggested by others.
AND
2) Add conformance to the UITableViewDelegate protocol.
If you do just the first step, the UIScrollViewDelegate methods will not be called.

Add the delegate lines in awakeFromNib instead of viewDidLoad. I had the same problem and that worked.

Related

textViewDidBeginEditing not working in IOS 9

i'm new in Objective C . What i'm going to do is simple just to animate keyboard up when textfield is focused and shrink when it's not. I already followed some other tutorial on how to set up this step by step but it not working.The event textViewDidBeginEditing is never get called when textfeld is focus.
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *cCodeTextField;
#property (weak, nonatomic) IBOutlet UITextField *jidTextField;
- (IBAction)nextBtnTapped:(id)sender;
#end
I'm also set the delegate to the textfield but it's not working.
- (void)viewDidLoad {
[super viewDidLoad];
[self addTextFieldBorder:_jidTextField];
[self addTextFieldBorder:_cCodeTextField];
_jidTextField.delegate = self;
}
-(void) textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"Enter");
}
It looks like your ViewController class is implementing methods from the wrong delegate. It is implementing UITextViewDelegate methods when it should be implementing UITextFieldDelegate methods.
Note that 'jidTextField' is of type UITextField.
Your delegate method is called 'textViewDidBeginEditing' which is a UITextViewDelegate method and takes a UITextView as a parameter.
The issue here is that your class is implementing delegate functions for UITextViewDelegate and not UITextFieldDelegate.
The correct method definition is:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
Here is a link to the documentation for the correct delegate:
https://developer.apple.com/reference/uikit/uitextfielddelegate
Hope this helps!

Detect when UIView is added in other UIView which MUST has a UIViewController?

Is there a notification or callback when a UIView is added in other UIView which has a UIViewController. We can get a UIView's ViewController, if it has one, via nextResponder. (Reference)
But depending on nextResponder is not reliable, if the UIView is not been added into a View which has ViewController, this method fails. For example, when we are calling from the cell's responder in UITableViewDataSource's cellForRowAtIndexPath. Because by the time of calling cellForRowAtIndexPath, the cell being dequeued hasn't been added into UITableView yet. However, we can call that method in - tableView:willDisplayCell:forRowAtIndexPath:, because by the time of calling - tableView:willDisplayCell:forRowAtIndexPath:, the cell is already being added into TableView.
So if you have ViewA which is the view of ViewControllerA and you want to add the ViewB you can subclass ViewB
#protocol ViewBDelegate
- (void) viewAddedToSuperview:(ViewB*)sender;
#end
#interface ViewB: UIView
#property (nonatomic, assign) id< ViewBDelegate > delegate;
#end
#implementation
- (void)didMoveToSuperview {
[super didMoveToSuperview];
[delegate viewAddedToSuperview:self];
}
#end
and in ViewControllerA you implements the protocol ViewBDelegate.
This is just an example of my idea.
Let me know in the comments it is can help you, otherwise I will try to propose other solutions depending on your goals.

How to use UIScrollViewDelegate methods on a tableView added to a UIViewController?

how could we use the UIScrollViewDelegate methods on a tableView that is added to a UIViewController? It works fine when I subclass the UITableViewController, but in my case, the tableView is added inside a UIViewController. The delegate and datasource are linked to self, I added UIScrollViewDelegate as a protocol, but the delegate methods are not recognized by the viewController.
Thanks
UITableViewDelegate inherits from UIScrollViewDelegate.
If you determine that object is delegate of table view, it would be also a delegate of scrollView.
Here is how to get this done in two steps:
ViewController.h
Your UIViewController must implement both the UITableViewDelegate and UIScrollViewDelegate. If you are working with interface builder, you will also have an IBOutlet for the UITableView:
#interface ViewController : UIViewController<UITableViewDelegate, UIScrollViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tableView;
ViewController.m
In your implementation code, you have to specify that ViewController is the tableview's delegate. You also need to implement one of UIScrollView's delegate methods to be notified of the scroll:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"Scrollview did scroll");
}

UITableView in a xib is not calling cellForRowAtIndex

I have a XIB that has a bunch of views in it (iPad). One of the views is a UITableView. The delegate was set via IB. When you run the app cellForRowAtIndex is not being called. As a matter of fact, in this file, there is also no numberOfRowsInSection or numberOfSections methods. There is only a didSelectRowAtIndexPath method.
So I wrote a cellForRowAtIndex and manually set self.table.delegate = self.
If I click a cell the didSelectRowAtIndexPath is executed.
I honestly am at a loss? How can this even happen?
Add:
self.table.dataSource = self;
cellForRowAtIndexis a datasource protocol method, so you must set the datasource to self as well in order for the controller to respond to the datasource protocol.
If this controller is anything other than a UITableViewController, do not forget to add the following to your .h file:
#interface myViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> {}
In your .h you need to add the Datasource and Delegate declarations
#interface HDMainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
and in you .m file where you create your tableview you need to set
tableview.delegate = self;
tableview.dataSource = self;
Now your dataSource and Delegate methods should work
I'm not sure what is causing the problem, however an easy fix is that in your -(void)viewDidLoad method you could call [self.table reloadData] which forces it to manually call all of the functions
Somethign else that occurred to me is that you said "numberOfRowsInSection or numberOfSections is not implemented"... Those methods are required for a table view and oyu must make sure to implement them. That could very well be the problem

UICollectionViewDataSource, UICollectionViewDelegate in UIView

I have created a UIView with UICollectionView.
In the interface declaration of the UIView I have conformed to the UICollectionViewDataSource, UICollectionViewDelegate protocols:
#interface TestOverview : UIView <UICollectionViewDataSource, UICollectionViewDelegate>
But when I run the app, it crashes and i get this error:-
error: -[TestScreenViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance.
Just to be clear the method collectionView:numberOfItemsInSection is in the UIView (TestOverview), which was declared to be the delegate of the UICollectionView. So why does it expect to recieve it in the UIViewController(TestScreenViewController), which contains the UIView that contains the UICollectionView?
First of all, it would be better if you used a view controller as the delegate of your UICollectionView. That's what they are there for. Second of all, not only you need to declare that TestOverview implements the UICollectionViewDataSource and UICollectionViewDelegate protocols, but also you need to tell the UICollectionView instance who their delegate and data source respectively are. You can either do it in code like this:
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
Or in the Interface Builder, by binding the dataSource and delegate items to TestOverview.
It is better to user viewcontroller as collectionview delegate than a uiview.
self.collectionView.delegate = self;
self.collectionView.dataSource = self;

Resources