Trouble adding buttons to UITableViewCell in Swift - uitableview

How can I add a UIButton into a UITableViewCell? I am having trouble doing this in Storyboards.

The problem is that the only prototype cell style you can modify in Xcode is a Custom style. Make that setting in the cell's Attributes inspector and all will be well.
I made a little screencast that demonstrates: http://youtu.be/bN-nBOgFL00
Note that there are two different things about a cell that can be important here: its style (in the attributes inspector) and its class (in the identity inspector). They are totally different things and have no relation to one another (they are "orthogonal", as we like to say). Something you said earlier makes me think you may be confusing them with each other...!

Related

Access child views in custom UITableViewCell

Background:
I am trying to create a table view with around 4 types of different cell layout.
At first, I considered using static table view to solve the issue since the number of rows are somewhat fixed (nor more than 10)
But, after some thinking, I decided that I don't really want to be tied up to the UITableViewController. Thus, I tried to implement it with dynamic table view.
Question:
After I create 4 prototype cells, I found out that I'll need to access the child views in cell (to update their value). But the only possible ways I know seem to be:
1. Create a subclass for each prototype cell, and create `IBOutlet` to the child views
2. Assign `tag` for each child view for later access
But I don't really like these two methods...
The first one is too cumbersome, and the tag in the 2nd solution does't seem to be very sepcific (access the child view by just some magic number..)
So, I would like to know:
Is there any better practice for implementing this kind of
tableview. (multiple cell prototypes, and fixed row numbers)
Is static table view a better way to do it? If yes, will there be
any limitations when I am tied up to UITableViewController?
For example, if I need more complex UI, and decide to add more views on to it, will UITableViewController be less flexible than UIViewController
Thank you so much!
If the cells are very similar but with different layouts they could share a common UITableViewCell subclass provided the class doesn't need to know the layout it is in just configure the available outlets.
If the code does need to be aware of the layout used then it is probably best to make them separate subclasses.
For Swift use is or as? to confirm the correct subclass for the cell (for Objective C it would be the isKindOfClass method).
1.You do need to subclass the UITableViewCell if you want to access his IBOutlets.
In order to distinguish between the cell just use isKindOfClass
2.It depends how different your cell are from one another. If they have slightly different structure you might want to consider lodging the elements in cellForRow. Try to take a look at the built in structure cause it might save you some subclassing.
These structures have built in parameters such as: image,text etc.
Their structure is more strict though
Theses are the available types:
UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1, // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2, // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
I don't really understand why you want to use tags?

iOS: Using custom UITableViewCell vs. Using Cell viewWithTag?

Custom Cell For UITableViewCell
I'm developing an iOS application and i need to make a custom cell for my side menu, i have seen so many examples and i found out those two approaches :
Make a class (.h and .m files) that inherits from UITableViewCell and put the outlets in the .h file then apply the class on the cell and connect those outlets to the labels and/or images from your cell in the storyboard.
Or the easy way is to give a tag to the labels/images or whatever you have in the cell in your storyboard.
My Question: I'm wondering which way is more accurate and professional and used by most iOS developers ?
Definitely the first one. It's clear and maintainable in the future. The purpose of the tag is not to store arbitrary values. See my answer regarding this.
This article has some details on the usage of tags

UITableView with 8 inherited cell type

I have a UITableView, with 8 differents type of cell. Those custom cell have some design in commom (like a upper left icon, a title, a subtitle...). However, under those common features, each cell is different. In order to have less to maintain, my 8 cells inherits from a default abstract cell with the commons IBOutlet.
Now my question is : what is the best, most proper way to do this?
At first, I thought of using registerNib:forCellReuseIdentifier: but this means 8 different xib. In this case, if one of the common design feature change, I would need to go trought all 8 xibs to change the same thing. I thnik it's not very productive and clean.
I thought also of registerClass:forCellReuseIdentifier: but this methods does not create the cell with a nib, so I would need to do everything programmatically.
The solution could be have one common xib, different registered class in the tableview, and those will be responsible for using their own custom design. But I can't see how to achieve this with those two previous methods.
I would use a single nib file with 8 different UITableViewCell subclasses. You should be able to use registerClass:forCellReuseIdentifier: and inside the class (which subclasses UITableViewCell) you make use of the nib file. If it is not clear I'll try to provide some code.

iOS : How do I slide a list item to expose commands (w/ storyboards)

I just started iOS development (I've been web dev for 6+ years) and I'm struggling with understanding how to build my UI beyond the default elements.
I feel like I started at kind of an awkward time. The resources I can find on creating a custom UI forgo the use of storyboards. Yet Xcode5 seems to force the use of storyboards (granted it's in beta so this may change).
While my current goal is to make list items slide over to expose custom actions (similar to mailbox, cobook, and countless other apps), really what I'm asking is for good resources on creating more custom UI's while still using storyboards that will lead me in the direction I need to go in order to do that.
Any help is appreciated, I've been desperately fighting the urge to take the easy way out and just use PhoneGap.
Thanks guys.
Check out this guide. In it they show how to make UITableViewCells with horizontal pan gestures so you can create the sort of effect that you are looking for.
http://www.raywenderlich.com/21842/how-to-make-a-gesture-driven-to-do-list-app-part-13
It uses a .xib file (I agree with you about the degree to which people using xib files in tutorials is incredibly annoying when storyboards seem to be the direction Apple is going in). However, you can make a UITableViewController in the storyboard. I would mostly use this in order to learn how to make custom UITableViewCells that can then be used in any sort of UITableView. You simply need to select the cell in the tableview, make the class of the cell the type of UITableViewCell you want (under the "Show Identity Inspector") and then make it the class of your custom UITableViewCell. Then when you call dequeueCellWithIdentifier, make sure the identifier is the same as the cell in the storyboard, and also cast the cell to your custom type like so...
MySlidingTableViewCell * cell = (MySlidingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
And make sure you #import the class for the cell.

ios - how to set global styles?

I am working with a storyboard, and I have some pages on the app that have content. Instead of manually creating styles for headings, and texts by using the right side of the screen little color/font editor, is there a way to just create a single style and use it throughout those content pages?
Thanks!
Since you only want the styles to affect some of your labels, I would suggest creating a subclass of UILabel for each different style you want. In each subclass, implement the awakefromNib method to set the style properties of the label. Then, for each label you want to have the style applied to, set the class in interface builder to be your subclass
Yes there is, but it would require you to use a stylesheet framework.
For example, three20 has a TTStyle framework, which is really good at creating style classes that you can apply to labels, buttons, and views.
http://three20.info/
Check out this tutorial:
http://www.mattvague.com/three20-stylesheets-tutorial
I believe Nimbus Kit also has a style library, but I have not explored it as much:
http://nimbuskit.info/

Resources