I created a header section for my UITableView following this post: https://stackoverflow.com/a/11396643/1568886. The issue I'm having now is accessing the buttons and label within the cell. When I connect the button and labels as outlets to the TableViewController Xcode throws an error:
/Users/ericky/Desktop/sample/sample-master/sample/en.lproj/MainStoryboard.storyboard: Couldn't compile connection: <IBCocoaTouchOutletConnection:0x7f8005e14430 <IBProxyObject: 0x7f8005e18260> => userProfileImage => <IBUIImageView: 0x7f8005e0d640>>
My view looks like:
How do I access the UI elements within the cell?
You need to create a custom UITableViewCell, create a class for it and connect the buttons and labels to the class you created instead of the TableViewController. I used this tutorial to learn how to do it:
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Related
Here is what I see on the storyboard. You can see the attributes of my table on the right:
Here is what I'm seeing after I launch the app:
I have not written any code that changes the view. It's all here in the storyboard. Everything is visible except the cells.
Xcode actually let you compile and run app with a tableview inside a UIViewController but It will show a blank table .. So instead of UIViewController embed static cell in UITableViewController . You can use ContainerView to get a static UITableView along with other controls on the same screen .. Embed tableViewController in containerView
Ray Wenderlich's website Blog Post says
One more thing about static cells, they only work in
UITableViewController. The Storyboard Editor will let you add them to
a Table View object inside a regular UIViewController, but this won’t
work during runtime. The reason for this is that UITableViewController
provides some extra magic to take care of the data source for the
static cells. Xcode even prevents you from compiling such a project
with the error message: “Illegal Configuration: Static table views are
only valid when embedded in UITableViewController instances”.
.
So everything is done the way you would assume. I drag and drop a collection view into a controller view. I connect the datasource and delegate. blah blah blah everything is done by the book.
Ok so i want to connect a label inside my collectionViewCell to my viewController class. I use control+drag to drop it into my code and type in its name.
As soon as i hit connect, i get a red octagonal error. it says: "Illegal Configuration: The hoursPerDayOutlet outlet from the monthCellViewController to the UILabel is invalid. Outlets cannot be connected to repeating content."
I need to connect this though because as each cell is created it will have a different label.
Please help me fix and use swift. All advice is appreciated. Thanks!
You can't connect items inside a UICollectionViewCell to a UIViewController, create a UICollectionViewCell subclass, make the class of the cell to the subclass and then you can connect the items inside the cell to the subclass (not the controller). I recommend you to look at more tutorials on UICollectionView.
To add to the other answer, after creating a UICollectionViewCell subclass and verifying that the class of that cell is subclassed, you have to make sure that your UIViewController that you were originally trying to connect the outlet to is a UICollectionViewDelegate subclass.
After that you should be able to successfully create an outlet for your label, etc.
I created a UITableView (and related classes), with two sections:
the first one is a static custom cell
the second one is a prototype cell
To control the static cell, I implement a class, and in that class I added IBOutlet to an object (for example label) in the static cell. When I attempt to access that outlet in the class controlling the table view, I get this error:
outlet doesn't exist
I made a video, for explain better my problem.
Short version
UITableView cannot be at the same time a Static Cells and a Dynamic Prototypes content. You need to pick one or the other.
Because you need Dynamic Prototypes, use Dynamic Prototypes.
Long version
Steps to create Dynamic Prototypes:
Select your Table View in IB > Show Attributes Inspector > Table View > Content > Dynamic Prototypes
From the section IB > Show the Object Library, drag as many Table View Cell onto your table view as you have cell types.
If you have some cells that never change at run time, you can very well create 1 instance of each right there in IB ; they will behave like Static Cells
For the custom cells(†), the recommended practice is to create subclasses of UITableViewCell, and apply that custom class to the relevant cell or cells (IB > Show the Identity Inspector), and pick your Custom Class from the list. This of course implies you have created such sub-classes.
Do not forget to assign a unique identifier to each cell prototype, so that you can retrieve these at runtime using dequeueReusableCellUsingIdentifier;
With this setup, you can select your Table View Cell, IB > Show Assistant Editor, and control-drag references in your custom class. You may need to tell the Assistant Editor which file to pick.
(†) There are plenty of other ways to do this ; all outside of the scope of this response.
I'm working on a Storyboards based app which constitutes of multiple view controllers linked via a Navigation Controller (initial view controller). This is used as the home screen. I'm using static cells and a static table view.
Now, I want to hook up some labels, buttons, etc as outlets/actions to "the .h" file and subsequently write custom methods in the .m file. But (as expected?) there's yet no custom class to select from in the identity inspector. So I created a new custom class as a subclass of UITableViewController (which seems to be the correct one?)
My new, custom class is now hooked up to my storyboard view controller. I then enter (what I believe to be) the correct numberOfSectionsInTableView: 1, and numberOfRowsInSection:5 (I got 1 section including 5 rows). When ran, it immediately crashes with the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
If return 0 sections and 0 rows, everything works fine (expect there are no rows displayed). Once I return anything > 0 in either of them, it crashes with this error.
What have I missed? Is there anything else I'm supposed to add to the file before it can be attached as a custom class and I can use it as any other custom class? Again, I've set up everything in the interface builder (storyboard), including the table cell rows, its styling, etc. What am I doing wrong?
My Table View Setup
My Table Cell Setup
Pretty sure this is the answer: in storyboard, make sure a prototype cell is painted on the table view (the one in the crashing view controller) and make sure it has a reuse identifier set to "Cell". Your code for rowAtIndexPath: is trying to dequeue a cell with that identifier and it's not there.
The identifier can be set using the attributes inspector (the middle tab on the upper right) in storyboard.
I have a tab bar application. In one of my tabs, there is a search bar and a table view below that. When you enter something into the search bar, it returns parsed xml. I need to put this parsed information into the tableview below. The class inherits from UIViewController. I declared a UITableView object in the header file and linked it in interface builder, and adopted the UITableViewDelegate protocol.
I'm not sure If i'm going about this the correct way. Any help?
That sounds about right. You will need to also have your UIViewController implement UITableViewDataSource, and add the methods from that protocol to populate the table.
There are various tutorials available which guide you step-by-step to create a simple tablview. Google them. You can follow these steps for connecting your tableView outlets:
Select your viewController where you are displaying the tableview to be your "File's Owner" in Identity Inspector.
Drag your view's outlet to your File's owner.
For tableView inside view, drag its outlets to File's owner again so that your datasource and delegates are up. And in the same view, drag your referencing outlet to the IBOutlet you have created in your viewController class.