Custom cell class in UITableView with static cells - ios

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.

Related

Is it possible to create an #IBOutlet in a custom view?

I've created a view on the storyboard in one of my view controllers and realized that I'll need to reuse it. So I then created a subclass for UIView to consolidate the code. I've selected the view in the storyboard and changed the class to my custom view class. Now I want to create #IBOutlets for a couple of the components in the view (in this case, 2 text fields). When I try and drag&drop from storyboard to custom view file, the only thing it allows me to create is an #IBAction and #IBOutlet is grayed out.
I tried creating the #IBOutlet manually in code and then drag from the circle to the component on the storyboard. Everything seemed to work (i.e. I see the connection in the little black popup and also the circle next to the variable is filled in. But when I run the app, the 2 fields are nil.
Is there a reason why I can only create actions and not outlets? (I'm kind of new to iOS dev).
No XIB is needed
Manually write the IBOutlet in your custom UIView class, for example:
#IBOutlet weak var container: UIView!
Open the storyboard and select the custom view in the Document Outline panel on the left.
Open the Connections Inspector on the right. Drag from the Outlet to the custom view in the Document Outline panel to connect them.
If you want to reuse a UIView then best to create an xib of the view and a custom class of the UIView. Then change the file owner in xib class to your custom class. Then you can drag and drop IBOutlets of the components inside the view to custom class. In the storyboard you can use the custom view and change the name of the class of view to custom view. Now drag and drop the IBOutlets from the story board to UIView. You can see the tutorial video on how to do , I have made for you.
https://youtu.be/IrgH522lbfA

Swift - Outlets cannot be connected to repeating content

The full error is: The playerView outlet from the TableViewController to the AVPlayerView is invalid. Outlets cannot be connected to repeating content.
Similar answers like this one have not worked (assuming I am attempting them correctly, I'm very new to iOS).
All I want is a view within a TableViewCell.
I've tried dragging a View in Storyboard to the contentView (and cell) of my TableViewCell, assigning it a class of AVPlayerView: UIView (and a subclass of tableViewCell: AVPlayerView: UITableViewCell), then ctrl+dragging from that view in the storyboard into my TableViewController. Then it doesn't compile. Really confused, thanks for the help.
Your table view can have static content or dynamic content.
If you want the table view to have always the same content, set it to static (in interface builder), and then you can link the outlets like that, in the UIViewController.
If you want the table view cells to change dynamically, you cannot do it that way. Because you could repeat cells and the outlet would be ambiguous. You need to create a UITableViewCell subclass for your cells, and create the outlets there.
To clarify: in dynamic table mode, you need to ctrl+drag the outlet into the UITableViewCell subclass, not the view controller.
Very simple solution is:
Just take the view or NSLayoutConstraint reference outlets in the subclass of table view cell instead of table view controller and access using object of table view cell in cellForRowAtIndexPath method or any other method.

Can't assign custom UITableViewCell within custom UIViewController

In my storyboard, if I have a UIViewController that inherits from a custom class, can I create a custom class for a UITableViewCell within a tableview inside that UIViewController?
I've tried to do the above, but when I try to connect outlets, I can only drag to the parent ViewController file, and not to the connected TableViewCell file. Why is that?
You can connect outlets of your cell.
You need to assign the class of custom table view cell in storyboard.
Select the table view cell while dragging to connect outlets.
You seem to be asking 2 questions:
"can I create a custom class for a UITableViewCell"?
Yes, you can use a custom class for your UITableViewCell. Simply select the cell and in the class inspector, enter the custom class.
"I can only drag to the parent ViewController"
That's also correct. For static cells, you can link them to outlets in the view controller. Note that for dynamic cells, you'll need to create them in the tableView:cellForRowAtIndexPath: method.

Static table view outside UITableViewController

After the new Xcode update, my app doesn't validate and shows this error:
static table views are only valid when embedded in UITableViewController instances
Any chances to solve easily?
The only way to get a static UITableView along with other controls on the same screen is to use a Container View. Follow this way, it works perfectly:
Drag a ViewController onto your storyboard.
Drag a TableViewController onto your storyboard.
Next Drag a Container view to your ViewController and size it about the size you want (smaller than the view) -> when you drag the container view it will create a segue and another view. Remove that segue and view.
Then finally ctrl click in your container and drag to your new TableViewContoller. Select Embed.
Style your TableView the way you want -> including static cells.
This answer was already found and given here: G.Huebner -> http://web.archive.org/web/20140928102504/http://iphonedevsdk.com/forum/iphone-sdk-development/111800-static-table-view-cells-only-work-in-a-uitableviewcontroller.html
A static UITableView must be in a UITableViewController. So you will have place the table in UITableViewController and then add it as childView to the MainViewController.
You can refer
https://iphoneidoit.blogspot.in/2013/10/static-uitableview-in-uiviewcontroller.html
I think what you are doing is you are first dragging the ViewController on the storyboard and then dragging the tableView on the ViewController. In this way you can't use the Static table cells. Instead of this what you should have done is Drag the TableViewController on the main storyboard instead of a ViewController and then select the static cells. In this way you can be able to work on static cells and can use any elements on static cells.
Well, if you are really using a static tableView, you should consider copy/past everything to a ScrollView or StackView.

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.

Resources