UITableView setter method functions not being called - ios

functions like this one:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(#"called")
return 1;
}
aren't being called, because the log doesn't appear at the console. I tried using
[table realoadData]
but it still doesn't work

Make sure that you have both the UITableViewDataSource and UITableViewDelegate set to the class that this method is located in
.h
#interface class_name : UIViewController <UITableViewDelegate, UITableViewDataSource>
{ UITableView *tableView; }
.m
in the viewDidLoad (or any other loading method)
tableView.delegate = self;
tableView.dataSource = self;
If you are subclassing UITableViewController, it should be working already
hope that helps!

In Nib file, you need to outlet tableview.
Your subclass must have UITableViewDataSource and UITableViewDelegate.
Assign tableview.delegate = self and tableView.dataSource = self;

Related

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

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.

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;

How can I subclass a UITableView?

I want to subclass a UITableView as I want to create a reusable table view component in my application.
The idea is instead of using a delegate for say cellForRowAtIndexPath I want the table view itself to get that call.
I don't think I want a UITableViewController as this UITableView that I want to build has to live in various UIViewControllers (and these UIViewController might have UITableViews of their own).
I subclassed my UITableView as:
#interface ShareUITableView : UITableView
but none of its methods get called.
My ShareUITableView is created via the NIB by setting the custom class to ShareUITableView. I have verified in code that a ShareUITableView is instantiated.
My UITableView does not delegate to its view controller, so that's not the problem.
Any ideas?
If I understood you, you need this class declaration:
#interface ShareUITableView : UITableView <UITableViewDataSource>
And then, in your class constructor, you should assign the instance itself as its own datasource:
- (id)init
{
//...
self.dataSource = self;
//...
}
Of course, the class will have to adopt the protocol.
Good luck!
MyTableView.h
// MyTableView.h
// This overrides the UITableViewDataSource with your own so you can add any methods you would like.
#protocol MyTableViewDataSource <UITableViewDataSource>
#required
// This is where you put methods that are required for your custom table to work (optional)
- (int)myRequiredMethod;
#optional
// This is where you put methods that are optional, like settings (optional)
#end
// This overrides the UITableViewDelegate with your own so you can add any methods you would like.
#protocol MyTableViewDelegate <UITableViewDelegate>
#required
// This is where you put methods that are required for your custom table to work (optional)
#optional
// This is where you put methods that are optional, like settings (optional)
#end
// Make sure you add UITableViewDelegate and UITableViewDataSource implementations.
#interface MyTableView : UITableView <UITableViewDelegate, UITableViewDataSource> {
// Your customer datasource and delegate.
id <MyTableViewDataSource> myDataSource;
id <MyTableViewDelegate> myDelegate;
}
#end
MyTableView.m
// MyTableView.m
#import "MyTableView.h"
#implementation MyTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// This is how you can use your custom method.
int i = [myDataSource myRequiredMethod];
}
return self;
}
- (void)awakeFromNib {
// This assigns the delegate and datasource you assigned to File's Owner in your xib to your custom methods
myDataSource = (id<MyTableViewDataSource>)self.dataSource;
myDelegate = (id<MyTableViewDelegate>)self.delegate;
self.delegate = self;
self.dataSource = self;
}
// This is an example of how to override an existing UITableView method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// This calls the method implemented in your ViewController. See Below.
NSInteger rows = [myDataSource tableView:tableView numberOfRowsInSection:section];
return rows;
}
#end
MyViewController.h
// MyViewController.h
#import "MyTableView.h"
// Use MyTableViewDataSource and MyTableViewDelegate instead of UITableViewDataSource and UITableViewDelegate
#interface MyViewController : UIViewController <MyTableViewDataSource, MyTableViewDelegate> {
#end
MyViewController.m
// MyViewController.m
#import "MyViewController.h"
#interface MyViewController ()
#end
#implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
// This method will be overridden by myTableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (int)myRequiredMethod {
return 2;
}
Subclassing is a great way to make reusable custom UI elements.
I think, you should still go with a Controller class. I expect subclassing UITableView to be tedious work — if possible with reasonable amount at all.
There is no problem to have UIViewController/NoViewController implemented the delegate and datasource and yet assign another controller to a specific tableView. note, that the datasource and delegate don't need to be subclasses of UITableViewController.
have a look at this answer: Implement Delegate at Run Time?
My UITableView does not delegate to its view controller, so that's not the problem.
You have to have to use delegate and datasource, that is how TableViews are filled and configured. otherwise you will have to overwrite every method of UITableView — including private ones, a no-go if you want into AppStore. Recreating UITableView without subclassing it would be even easier.

Resources