Outlets found nil after moving tableviewcell to new view controller - ios

I originally had several UITableViewCells on a UIViewController with a UITableView. Then, I moved the UITableViewCells to a new UITableViewController I created because I wanted to make them static cells and the only way to make them static was to use them on a UITableViewController. However, after I moved, them, although everything was connected and all the cells were set to the right xib files, the app crashed and it said the outlets on the UITableViewCells, like my UILabels, were found nil. I then tried to create brand new UITableViewCells with brand new xib files on the UITableViewController, but then I couldn't even connect any outlets for any UILabels or anything else. Does anyone know how to troubleshoot this? All my code looks normal and I've been using UITableViews for a long time, just never creating static cells before.

I figured it out! In a UITableViewController with Static Cells you don't need to create a custom class for the cells and drag your outlets there (what I did); you can just drag your outlets directly onto the UITableViewController and it will work there.

Related

iOS - UITableViewCell in different Storyboard

I want to use a UITableViewController which has four different type complex cell. Different team members are required to work on different custom UITableViewCell simultaneously. We are working on Git so to avoid conflicts I have been asked to make four different Storyboards for four different cell. But I cannot place a cell or simple UIView in different Storyboard without view controller. Please guide me, how do i implement it.
Please keep in mind that cells will be instantiated and added on
runtime
Create xib of your each cell, and register it in your UITableView , you can use this xib anywhere in your project and with the same CellIdentifier.
How to Make UITableVIewCEll XIB

How to use a static UITableView inside a UIViewController?

I know that it is not possible to add a static UITableView inside an UIViewController (or at least I could not find any way to do it). Trying to do a workaround (as pointed here), I am following these steps:
I have created an UIViewController on my Main.storyboard.
I have drag an UIContainerView inside my UIViewController.
I have deleted the UIViewController embedded by default on my UIContainerView.
I have created an UITableViewController and drag it to the UIContainerView.
I have added a label with the text "Hello" into my UITableViewCell.
Here is the structure of this part on my Main.storyboard:
And here is the result that I am getting:
I can only get an empty UITableView without any information inside of it.
Am I missing something? Am I doing it in the wrong way?
P.S: I am using Xcode8 and Swift3.
Thanks in advance!
You are using a UITableView with Prototype Cells which is visible because in the grey area it's written "Prototype Content".
Change to Static Cells in the Table View inspector (on the right of the window) after selecting the TableView in Interface Builder.

Custom cell class in UITableView with static cells

I have a UITableView with static cells in a Storyboard. The static cells are of a custom class. I would like to wire up the outlets for the cell but it seems like this is not supported?
Wiring the views onto the ViewController works, but is rather messy (there are lots of cells). A workaround would probably also be to tag the views, but also not ideal. Any other ideas?
You cannot control-drag your UIView to the source file. However, you can link up the other way around.
In Storyboard, select your static cell, and setup the Custom Class.
In your custom class, add all the #IBOutlet as you need.
Go back to Storyboard, select your static cell, and in Inspector, select Show the Connections Inspector (the last tab).
Drag the Outlets there to your UIView(s).
If you use "assistant editor", you will see the circle in front of #IBOutlet is now filled (hooked).
have you checked custom class of that UITableViewCell. it should be custom file and subclass of UITableviewCell you are created.

ios 7 customizing UITableViewCell's content view

Using storyboard in ios7 the content view is explicitly viewed under the Table View Cell in the story board editor (opposed to previous versions where it was hidden from the interface).
The problem is that I cannot connect the cell's custom elements to my custom cell's ib outlets anymore (subclass of UITableCellView) and can only connect them to the table's content view (UIView) which CANNOT be customized (i.e. its custom class attribute is grayed)
This leaves me with the option to use tags which I find inconvenient and less elegant.
I could also create a custom view in a xib, do all the connections and then manually add this custom view as the content view of the table but I am looking for a way doing it via the story board editor.
Is there a way to connect UI elements to a custom cell's content view in the story board editor in ios7 ?
Ok I found an Xcode bug.
If you complete the following this will replicate the issue:
- Create new UIViewController in storyboard
- Drag a UITableView to the VC
- Update the UITableview to have 1(as many) dynamic prototype cells
ISSUE: The cells are added but without a contentView.
RESOLUTION:
Rather than updating the amount of cells in the storyboard.
Drag a custom cell from the objects part of Xcode, the Cell will be added with a contentView.
I was able to do this by doing the following:
Select the cell in the document outline
Change its custom class in the Identity Inspector
Place whatever elements you want into the content view
Connect the IBOutlets to the elements inside the content view using the Connections Inspector
I don't really know the answer, but I can suggest work around this issue:
1) Copy existing cell from other tableview to the one you're working on.
2) You will have contentView under your cell now. Design this cell by adding your views.
3) Create a class for your cell, e.g NewCell, then manually create IBOutlet in this class:
#property (nonatomic, strong) IBOutlet UILabel* mainLabel;
4) Assign the cell class in storyboard to the class you just created. After this step, you can drag the outlet from storyboard to class.
I'm not sure if this is a bug for XCode 5 or it is intended, and I'm looking for better solutions as well.
Create new uiviewcontroller with xib interface and add the required sub-views .Also,establish the iboutlet connections.
Now ,rename the viewcontroller : UIViewcontroller to viewcontroller : UITableviewcell in .h file of your newly created view controller.
Your tableviewcell was created and ready to use with any UITableview of any class.
Hope it helps.
I might be duplicating someone else's answer but I found this workaround which helped bypass this silly XCode bug. Short solution: When you build your custom cell in Storyboard, do NOT drag your UI elements inside the cell or content view.
Instead, drag them OUTSIDE so that they're child elements of the parent table view!
It's easiest to do this drag and drop if you use the little hierarchy menu on the left to make sure your elements wind up in the right spot.
Once the elements are dragged in, just control-click and drag from the custom cell to the UI elements to make your connections. Woo hoo!
Once your connections are set, then (finally) drag the UI elements back INTO the custom cell's content view and lay them out as you normally would.
UPDATE: While my method works, look for a comment by thomasdao in an answer below where he just drags the connections directly into the .h file (the little circles in the left margin where you declare your IBOutlets) -- this is actually the easiest solution but unfortunately it's stuck as a comment.

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.

Resources