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

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");
}

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.

UITableView scrollView delegate not called

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.

UIViewController, UITableView, Protocol

I have a UIViewController, which has a
#interface MYViewController : UIViewController<UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tblView;
Now I am implementing the UITableViewDelegate because I want to use the tableview.
so in my viewdidload I do
- (void)viewDidLoad
{
[super viewDidLoad];
self.tblView.delegate = self;
//self.tblView.dataSource = self;
}
Now the Datasource setting throws a warning, and I never hit the
cellForRowAtIndexPath
method.
Sorry about the editing.
What am I doing wrong?
you need
self.tblView.datasource = self;
also adopt to UITableViewDatasource Protocol,
#interface MYViewController : UIViewController<UITableViewDelegate,UITableViewDatasource>
cellForRowAtIndexPath is a datasource method.
Your interface should also implement the UITableViewDataSource protocol.
#interface MYViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
....
}
Please try as below..
UIViewController.h
#interface MYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
#property (Strong, nonatomic) IBOutlet UITableView *tblView;
please don't forget to connect tblView outlet to tableView in your .xib file, since this is very required otherwise your datasource methods won't get called.
In UIViewController.m file
- (void)viewDidLoad { [super viewDidLoad];
self.tblView.delegate = self;
self.tblView.dataSource = self; }
and implement two required datasource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return (number of row you want in table)}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ return cell }
surely this will work for you.
For UITableViewDatasource Protocol add UITableViewDatasource to
#interface MYViewController : UIViewController<UITableViewDelegate, UITableViewDatasource>
and add this line to viewDidLoad
self.tblView.datasource = self;
and go to your storyboard and link 'datasource' and 'delegate' to your tblView
There are couple of problems in your implementation.
You are not conforming to UITableViewDatasource protocol.
You are not setting tableview datasource.
You are not implementing UITableViewDatasource protocol's required methods
You cannot expect to work without setting datasource.

UITableView inside a UIView

I would put an UITableView inside a UIView. I created a XIB where I have a tableView.
#import <UIKit/UIKit.h>
#interface PointsViewController : UIViewController <UITableViewDelegate>
{
IBOutlet UITableView *tableView;
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
[tableView setDelegate:self];
}
I instantiate the PointViewController class from another class and add it to a UINavigationBar by means of a button:when I click the button, the PointsViewController'view (the tableView) shall open. But it does not.
What am I missing? I tried also to make PointsViewController as a subclass of UITableViewController which works, but no UITableView is displayed.
You will also need to make your ViewController a delegate for UITableViewDataSource.
#interface PointsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tableView;
}
#end
...and support the corresponding methods.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
You need to also hook up the table's dataSource and delegate to the File's Owner. Otherwise the view controller doesn't know what table to send responses to.
In your XIB, select the table and open the Connection Inspector. Then drag the 'plus' sign next to dataSource to File's Owner to make the connection. Do the same for delegate and the table's referencing outlet.

Resources