Cells not appearing when linking table view controller to table view file - ios

Very new to iOS dev so a little help would be greatly appreciated !
Created a table view controller with static cells in it. If no swift file is linked to it, I can see the cells when building the app.
However, if I link a table view controller to it (which I believe I should?), I don't see the cells anymore. Why is that?
Thanks !

There are several things you have to keep in mind when using Table View.
First, you have to make sure you link the table view to your file by control dragging it from the main.storyboard to your view controller.
Second, you must make sure that the cell has an identifier. Make sure you select the cell and not the table and set the identifier to a certain name (for simple projects, we generally just use the identifier "cell")
Third, you have to make sure that the datasource (and usually the delegate) is set to the view controller.
There are two ways of doing that, first, you can drag the table view to the View Controller icon (I believe it is the yellow one, but one of those on the top of your iOS screen storyboard), and select datasource and delegate. The second way is after you have referenced your table view to your view controller file, in the viewDidLoad method, set the table view's delegate and datasource to self. For instance, if your table view is named as tableView, the code should look something like
tableView.delegate = self
tableView.dataSource = self
Forth, make sure you include the following methods in your class. The numberOfRows, cellForRowAtIndexPath, and for numberOfSections, just return 1.
If everything still doesn't work, you can share us the code, maybe there are some other potential problems I forgot.

Related

iOS Different Views for Table View Cell items

I just started iOS development last week and I am creating a Table View based application. I having trouble understanding how to use storyboard.
I want each Table Cell to open a different ViewController.
Currently it is setup like this:
Then in the Component View Controller I use if/else statements to determine what content to load. The problem occurs when one of the views needed a TabBar.
How do I assign different View Controllers to each individual cell, rather than one "template" view and forced to add everything dynamically.
First off, I will say that it probably isn't the best idea to assign an individual view per cell. However, if it is what you wish to do, then so be it.
What you would do is create a segue from the table view to the new view by clicking on the table view icon and dragging a segue like so:
2.You would give that segue an identifier like "embedTweetsSegue" or something.
You can then check for the cell being touched and perform the segue programmatically using:
performSegueWithIdentifier("embedTweetsSegue", sender: self)

How to create a separate view in Storyboard to be included programmatically in UITableView?

I have a UIViewController with a UITableView that is fed with data from the local database. When the user first launches the app (after installing) the table view is empty and I display a UIView in the middle of the table view that contains a UIImage, a UILabel and a UIButton (a call to action).
The first version of this view I built programmatically, which was no good to me because every time I tweaked something I had to build the app again. Then I switched to the storyboard but had to drag a UIView to the middle of my tableView. It is working now but I don't like the way it is, I can't edit my table view cells without having to move the UIView out of the table view.
I'd like to have a way to build this view entirely separated from my tableView (or even from my view controller in question) and then reference it in the viewDidLoad call of my view controller.
Unfortunately Xcode does not allow us to drag views directly to the storyboard so I'm pretty lost here.
Please tell me if I haven't been clear enough. I appreciate any help you could give me.
UPDATE: It'd be particularly awesome to me if I could also create a custom Swift class for this view of mine and reference it in the storyboard. And then in my viewDidLoad I could simply instantiate my custom view.
Thanks in advance.
Create a XIB file in which you can drag a view (without a view controller).
In your code you can load the XIB using NSBundle.mainBundle().loadNibNamed("MyXibName", owner:self, options:nil).
In the XIB file you can give the UIView a custom class (like you can do with view controllers in storyboard).
You then of course have to retrieve the view from the array returned by loadNibNamed and cast it to your custom class.

Is it possible to re-assign an existing UITableView to a different UITableViewController?

Most of my app is created programmatically, but I have a handful of complex views that are created using Interface Builder and loaded programmatically.
In one view I have a UITableViewController subclass created programmatically, and its tableView property is assigned to the UITableView instance in the XIB, referenced as an IBOutlet. This happens in the XIB's UIViewController's viewDidLoad, and the table view displays correctly.
At one point, I need to change the entire contents of the table view. Ideally, I would like to disconnect that view from the first UITableViewController and connect it to a different one that will feed it different data. Attempting to assign the view to the new controller's tableView property results in "A view can only be associated with at most one view controller at a time!" Assigning the previous controller's tableView to nil before assigning the new one avoids the error, but the table view disappears from the screen.
Honestly, I know the table view instance is usually managed by the UITableViewController. I can't find anything in the docs about setTableView automatically setting the back references for delegate and dataSource in an assigned tableView, so I'm surprised my externally-created tableView works in the first place. In fact, the TVC docs seem to discuss UITableViewController's tableView property as readonly, even though it is not tagged as such.
I'm sure I don't have all your information, but on the surface it seems that you are making this more complicated than it needs to be. Why don't you just use the current UITableViewController and corresponding UITableView and reload it with "new data". Alternatively, just programmatically create another UITableViewController and use that for your new data. Is there a compelling reason to recycle the UITableView?
The answer is no, and here's why:
I checked with the debugger, and the normal setTableView does set the view property on self as well as setting the delegate and dataSource properties on the new incoming tableView. This is not documented by Apple, but it was the assumed behaviour and so it was all well and good.
What I did not realize is that the default setView (from UIViewController) will remove an existing view from its superview before setting the new one. To prevent the "one view controller at a time" runtime error I was setting the old TVC's tableView to nil, which caused the tableView I intended to reuse to be removed from the view hierarchy. To avoid this behaviour, one would have to override setView, which is more complicated than I care to get into for this task.
Reusing views (in this case, a UITableView instance) is not supported by Apple. It is not mentioned in the UITableViewController documentation, but it is mentioned in the UITableViewController documentation regarding the view property. Arguably, UITableViewController's tableView property should be marked readonly by Apple, with its tableView solely managed internally, but there's probably a good reason why its not.

how to implement multiple segues using dynamic cells in Xcode?

i have a table view with dynamic prototype cells divided into 2 different sections named "Forums" and "Threads". When i click on a tableview cell from the Forums section, i want to transit to the same page with a different data to display while if i click a cell from the Threads section, it should open a different scene. In short, two different types of transition segues from 2 different tableview sections)
Can anyone please help me with this?
Make sure the table view controller is embedded in a navigation controller.
Hook up two push segues to the table view controller (not to the cells).
Give those segues appropriate identifiers in the storyboard using the attributes inspector tab on each segue.
In -tableView didSelectRowAtIndexPath: add an if statement to detect which section the row tapped was in.
In the branches of the if statement, call -performSegueWithIdentifier on your controller using the identifier of the appropriate segue .
If you need to set up anything in the view controller you're seguing to from the table view, override prepareForSegue: sender: in the table view controller.
Very helpful, it worked for me, but I had to used BOTH didSelectRowAtIndexPath AND prepareForSegue, while I was reading in other posts that this is not very good.
I read that we need to use only the prepareForSegue if we need to set up any stuff...
Do you think that I should continue with both of them?
Thanks!

UITable view interface builder creation

I am building an app using storyboard. I have added a table view to my view controller, chose static cells, and custom, insertea a title and some pictures. I have also linked each row to a view controller, with modal segue. But I want to be able to add, delete, and reorder the rows. So I added the codes. The only thing is that I can not connect the table view to the codes. If I connect the data source, and run the project I see just an empty table view. So, is there a way to create the table view using interface builder, and just a few codes? otherwise if there are to much codes everything gets messed up.
Connect delegate and datasource to ViewController icon in your Storyboard. Then add UITableviewdelegate and UITableviewdatasource in view controller code. When you connect and do this, then tableview will search its delegate and datasource methods in your code and you will get the output which you expect.
If you want to add, delete and reorder the rows, you can't use static layouts, because those operations make the table view not static!
You will have to implement it using datasource methods and dynamic prototypes. It won't be too difficult. You can still build each type of cell in the storyboard, you can have multiple prototypes with different reuse identifiers.

Resources