compiler not running tableview datasource methods - ios

I met a question that i ensured setted datasource and delegate ,implemention two datasource methods ,but compiler not runnin this two methods,have someone tell me why?

When you assign self to tableView's dataSource, you have to make sure your tableView is not nil, then everything will be ok.

Related

Migrate UITableViewController to UITableView

I currently have a UITableViewController, but would like to add a label at the bottom of the screen. I think the easiest way to do this would be to just use a UIViewController with a UITableView. Is there an easy way to migrate over? I've noticed some functionally is different; for example UITableViewController has a numberOfSections method, whereas UITableView does not have that method. Also is it possible to use a UIViewController as the delegate for a UITableView? Thanks in advance.
Update: I have attached a picture of the code, and the error given when trying to implement UITableViewDataSource.
You can do this. You need to implement the UITableViewDelegate and include the UITableViewDatasource in your View Controller, then you can use the functions you were using in your TableViewController.
Basically what you should do is add a tableview in the interface builder, or programmatically (depending on your choice). If it's done through the interface builder then outlet it to your Controller. Add the Delegate to your class, and you would have access to the tableViewFunctions (cellForRow, numberOfItems, numberOfSections, didSelectRow etc...).
Also, when you create the tableview specify the tableview.delegate = self and tableview.datasource = self.
As far as I know, the methods are the same, you just won't need the override bit before each one if its just a UITableView. You can use UIViewController as the delegate, yes, by setting myTableView.delegate = self in its viewDidLoad method.
If its just a label you need however, you might consider adding one programatically? Similar results achieved with a no data label for example - see here. You could then play around with the positioning.
This error is thrown because you need to set dataSource to your TableView Object like self.tableView.dataSource = self. After this you can use freely all methods for UITableView including numberOfSections as you mentioned.

didDeselectItemAtIndexPath function is not being called in programmatically created UICollectionView

I have created a UICollectionView programmatically. Its cells also contain programmatically created UILabel, UIView and UIImageView. The view that contains this collection view has also been created programmatically. I've set its datasource and delegate to self. The userInteractionEnabled property of label, view and image view has also been set to false. The allowsMultipleSelection property of collection view is also set to true.
The problem is that the didSelectItemAtIndexPath function is called whenever I touch the cells. But didDeselectItemAtIndexPath function is never called. I tried to set the cell.selected property to true/false as required but no aid to my problem.
Please help!! I've spent many hours searching about it but no result!!
Thanks for you responses.
Well, both this functions are called according to UICollectionViewDelegate in object which conforms to it. I highly advertise you to check this points:
After creation your custom collectionView don't forget to set it delegate property.
Implement this methods in your delegate object. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath: and put there simple NSLog() output or breakpoints
Don't forget to set up UICollectionView property allowsSelection to YES
Face the result
Important notes
1. If you call UICollectionView methods like -selectItemAtIndexPath:animated:scrollPosition: or -deselectItemAtIndexPath:animated: any of this action will not be triggered nor notification will be received. As you remember, the same as in UITableViewClass
2. UICollectionView property allowsMultiplySelection allows you to switch between collection cells states
3. It doesn't matter where you create you elements. It just need to be setup correctly.
Hope this may help you.

UITableViewController subclass implements UIDataSourceModelAssociation protocol but its methods are never called

My app uses the Stanford course's CoreDataTableViewController, which is a subclass of UITableViewController.
For particular views of my app I create subclasses of CoreDataTableViewController to manage a particular view.
I've just recently hooked up state encode and restoration throughout most of my app and it all seems to be working ok. The only thing I've left late is implementing the UIDataSourceModelAssociation protocol to allegedly preserve visible and selected rows of UITableViews (I was also hoping it may preserve the editing state of the table view, and of a particular row if I've selected it to delete, but haven't confirmed yet).
I implemented the UIDataSourceModelAssociation within CoreDataTableViewController hoping that it would just then work for all my views, but on debugging it the feature doesn't work, and adding breakpoints I see that neither of the two UIDataSourceModelAssociation methods are ever called when i put my app into the background with the home button, or restore it cold by re-running in XCode.
CoreDataTableViewController and my subclasses of it implement some dataSource methods to actually display the rows in my table view. I've searched around and found that UITableViewController automatically adds itself as the dataSource for the tableView.
I've added sanity asserts in my subclasses viewDidLoad functions of the form
assert(self.tableView);
assert(self.tableView.dataSource == self); <--- fails here
Which fails on checking the dataSource being ourselves. I can see by logging and using the debugger that the dataSource is non-nil at this point.
I guess I just wondered whether when we subclass a class does each class share the same self value, or is it because my UITableViewController has set itself to be the dataSource, but by the time I've subclassed it twice down to my particular view instance it's self is slightly different than the parent UITableViewController. Or is something else intercepting the dataSource and forwarding on as required. As a fix should I consider hardcoding my own subclass to self, or have the CoreDataTableViewController add itself as the dataSource explicitly as this is where the UIDataSourceModelAssociation methods are implemented.
For reference my view hierarchy consists of my UITableViewController subclassed views living inside NavigationControllers which I've heard may cause issues.
Just a little of clarity on how subclassing should work with respect to self pointers across the classes and wondering if anyone could figure out why my protocol methods aren't being called would be really appreciated.
Cheers
I'm still not sure why my tableViewController's dataSource isn't directly set to self. But clearly my dataSource methods are called as my tableView functions. Obviously some sort of object is forwarding on dataSource methods to my actual class for me...
In the end getting the UIDataSourceModelAssociation protocol methods to be called was as simple as ensuring that both the UITableViewController and the UITableView within it had restoration ids.. with just the outer UITableViewController having a restoration id, none of these methods are called.
Sadly now I'm scuppered by these methods being called before my UIManagedDocument has finished loading so I cannot actually do anything useful within them. That's another problem though.

cellForRowAtIndex method not getting called?

In my application I used two tableViews. For the first table all delegate and datasource methods are called. When I add the second table and call the method
[self.tableView reloadData];
cellForRowAtIndex method is not called. In the second table, the number of sections is 1 and the number of rows is 5. I set the delegate and datasource for tableView programatically.
It can be due to any of the below resons :
You have not connected the outlet to second table you are reloading.
You have not assign delegate as yourtable.delegate= self;
So if there is any of the missing case fix it.
When NSAssert(self.tableView.dataSource, #"No datasource"); is crashing, you haven't set the dataSource of the tableView correctly.
The NSAssert will crash your app if the condition inside the parenthesis of the assert validates to false (or 0, or in this case nil).
Wherever you create your tableView, make sure that you set the dataSource (and probably delegate too) correctly.
Regarding setting the dataSource correctly, don't make the mistake to set the dataSource to a newly created instance of your viewController. Most likely you want to use self.tableView.dataSource = self;.

Reload UITableView with a different number of rows

I've got a UITableView set up and working, and now I have a little issue. I need to reload the tableView's data whenever the view loads, and one thing which I can't get working is attempting to reload the data, when there was no data in the table view to begin with.
Basically the list is of variable length, and the contents are loaded from a file, what I want to know is, is there I way I can force the table to reload and not have it ignore the datasource methods, as is what happens whenever [tableView reloadData] is called.
[tableView reloadData] relies entirely on the data source methods! So, the only way you would see your data source methods beind ignored would be that you have not set the data source (and perhaps delegate) of your table view to be the object you want to be the data source. You can set these through Interface Builder, or programatically, e.g.:
tableView.dataSource = self;
tableView.delegate = self;
whenever the view loads
Where do you call reloadData? Do you call it in -viewDidLoad method? If yes it is probably incorrect, because -viewDidLoad is called only once - when view of the corresponding UIViewController is created (on the first usage). Maybe you should take a look at -viewWillAppear which is called(assuming correct usage of UIViewController) whenever view is going to be displayed.
The other possible reason is that if you do use -viewWillAppear (or -viewDidAppear) it is not triggered at all. This can happen if you use a custom UIViewController hierarchy. In that case you must call it with your own hands (there are exceptions - UINavigationController for example does this for you, but simple [someView addSubview:myController.view] doesn't).
Also please check if delegates are set correctly and tableView is not equal to nil (as you know messages to nil are just ignored).
datasource methods aren't ignored when u call [tableView reloadData];
Did u set the IBOUTLET and the sources right?

Resources