IOS: How do I populate a UITableView which is within a nib? - ios

I have a nib file in which there is a UILabel and a UITableView.
So I've created 2 outlets - one for the label the other one for the UITableView.
Then I've created a subclass of TableViewController which knows how to populate a UITableView the way I want to. With it I'm going to populate that my UITableView.
Then in viewDidLoad of the controller for that nib file I do
[myTableViewController setView: myTableViewOutlet]
I expect the functions numberOfRowsInsection and other such from myTableViewController will be called after that so my UITableView gest populated.
But this does not happen and my UITableView remains empty..
What do I do wrong?

Two things I can think of that might be missing:
Set data source and delegate of the tableview to the controller;
Call reload data method on the tableview to trigger a reload.

Related

Table View's Delegate Method Cell for row at not called

I am using a table view and set the delegate and datasource in view will appear.
return 1 in number of section and 5 in number of rows but cell for row at delegate method is not called. I checked my storyboard view cell's ideantifier and class name is correct. Pls help
There are few things that may be or are wrong with your code. First one is that you should setup the delegate in storyboard or ViewDidLoad (in storyboard drag from tableView to the ViewController instance and select both - delegate and datasource) The other thing is that you may made a typo when writing the code for delegate methods(always use automatic method completion feature in xcode)
After setting delegate and datasource , reload the table view.

UITableView does not show any cells after UIViewController popped then returned to

I have a UITableView within a UIViewController and am having an issue where the table displays ok on first load via a segue, but when you push the back button on the header, then go forward to the UIViewController again the table is blank.
I'm using a custom classed tableview, but the default methods like numberOfRowsInSection and cellForRowAtIndexPath are being called on the second load. In fact, the cellForRowAtIndexPath method is returning a cell with the cell.textlabel.text properly set as expected. numberOfRowsInSection is returning the correct number of rows. And numberOfSectionsInTableView is returning 1. But when everything finishes loading there's nothing in the table.
I'm using storyboards and the prototype cell is set up with a reuse identifier. I've tried calling [tableView reloadData] from the view controller in both the viewDidLoad and viewWillAppear methods.
The datasource and delegate are both set ok (I think - or else it wouldn't work first time it's loaded).
I'm not doing anything different (that I can see) to other tables in my app, apart from using the UITableView within a UIViewController rather than using a UITableViewController. Is there anything that I'm missing that the UITableViewController handles automatically?
I'm a bit stumped - any ideas would be much appreciated!
EDIT: The custom class used in the UITableView isn't under ARC control, as it's an old library that I'm using. I'm using the -fno-objc-arc flag. Could this be something to do with it?

How do I have multiple UITableViewControllers on the screen at once? My table just shows up blank

My goal is to eventually add multiple UITableViews to the screen at once and allow the user to scroll horizontally between them like columns. Right now to show my tableviewcontroller I set my custom controller to the root controller:
[[self window] setRootViewController: myCustomController];
This works fine for showing one table on the screen. However when I try changing this to:
[self.window addSubview: myCustomController.view];
Then I can see the background of my table but it doesn't load any cells into it. Likewise if I do:
[self.window addSubview: myCustomController.tableView];
then it just loads a blank table again like so:
I also tried changing the header of my custom controller to extend just UITableView instead of UITableViewController however the exact same thing happens when I try inserting the view, it's just a blank table.
I tried putting NSLogs in some of my methods and it seems like without directly inserting the controller these methods don't seem to run:
heightForRowAtIndexPath
cellForRowAtIndexPath
numberOfRowsInSection
All I want to do is figure out how I can insert multiple different instances of my custom UITableViewController on the screen so that I can use either of these:
https://github.com/alekseyn/EasyTableView
https://github.com/TheVole/HorizontalTable
To put them all side by side and display information from different data stores.
Your problem is that you are not setting the delegate and datasource for the table view.
You could do this in code:
//assuming outlet named tableView
self.tableView.delegate = self;
self.tableView.dataSource = self;
Also, you will need to modify your corresponding .h file:
#interface something : somethingElse <UITableViewDelegate, UITableViewDataSource>
....
If you are creating your table view through IB, make the same change to the .h file, but instead right-click drag from your table view to the little orange circle at the bottom of the 'scene'. You'll need to do this twice; once for the dataSource and again for the delegate.
Well you have to set your table view datasource and delegates before you can load any data in it.
self.yourtableview.delegate = self;
self.yourtableview.datasouce = self;
if you don't do this your tableview wont be able to know where its instance methods are implemented in your code and won't be able to send them a call.
Now about your redraw problem until you don't tap on screen. Try calling
[self.yourtableview reloadData]
where you have set yourtableview's datasource and delegate. Also, if you change your contents of tableview to make them appear on your view. You will have to send a call to your reloadData instance method again.

How to use a TableView inside a UIViewController?

I want to add a simple static TableView to my UIView. So i dragged it into my UIView. Added the TableViewDataSource and Delegate Protocols to my ViewController class. Created an outlet to my tableview and connected the datasource and delegate outlets to the viewcontroller.
But i still get an error message which says that Static table views are only valid when embedded in UITableViewCOntroller Instances ? Any Ideas how to solve this Problem ?
PS: I am using UIStoryboards for designing the UI
Use a containerView (drag one onto your UIView) which will link to a UITableViewController instead. You will use the prepareForSegue method to initialize the table view controller.
Oh, and you might want to accept answers that help you or no one will help you anymore.
If you want static cells, in your Storyboard, click on the tableview, then in the inspector, change the Content from 'Dynamic Prototypes' to Static Cells. You can also segue these cells to a new controller without having to set up data sources and delegates for the table.

IOS SplitView TableViewController query

I have one SplitViewController with default settings added into an xib file. Its Table View controller i defined as another class (in Identity Inspector) as a TableView Controller class I created.
Now I have put some code in its
...numberOfRowsInSection:(NSInteger) section
and ...cellForRowAtIndexPath:(NSIndexPath *) indexPath
It works fine with total values in its table according to numberOfRows I have defined and elements in cell according to what cells I create and return in the appropriate method.
My question is:
How is table coming into the SplitView because when I customize UITableView in the xib of my TableViewController, its changes do not appear in the SplitView's left section. (I guess that table View is non-customizable? )
Secondly How does iOS initialize that TableViewController class from XIB because I have to put some initialization code that when that TableView is created, do following things. I tried over-riding various init functions it does not call any of those. How can I initialize some data when that TableViewController is initialized?
Thanks.
The table view is customized in the XIB that contains the split view and not in an own XIB file
You can do 2 things: override initWithCoder: (remember to use the self=[super initWithCoder: pattern) or override - (void)awakeFromNib

Resources