A few days ago, I watched the video tutorial which explains how to use custom cells in an UITableViewController. I've learned that I can prepare a custom cell directly in the interface builder, so I did following:
I created a UITableViewController and connect a custom class which consists of an IBOutlet (UILabel). After that, I switched in my storyboard and prepared my custom cell with an UILabel. Finally I connect the label from UITableViewController to my custom cell directly.
The following happens:
Couldn't compile connection: <IBCocoaTouchOutletConnection:0x400724860 <IBProxyObject: 0x4007872c0> => productLabel => <IBUIImageView: 0x401080220>>
Isn't it possible to connect this directly? In the tutorial video of WWDC 2011 Section (Session #309) they do exactly what I did. But there is a hook: they don't show the code behind, they just connect it like I explained above.
For a better understanding I add a screenshot which shows, what i connect:
I had what turned out to be the same problem in this question. As the answerer of my question said, the problem is that the cell here is a prototype cell. A connection between a cell element and UITableViewController works fine for a table view with static cells since they are created at launch, but it doesn't make sense for prototype cells since many of them will probably be created...and they don't exist until cellForRowAtIndexPath is called. (This is a really poor error message, and Xcode probably shouldn't let you make a illegal connection like this.)
Actually,
I have seen the video and they are not connecting the way you suggest. They are connecting from the cell to the UILabel in the cell. In other words, they have a subclass of UITableViewCell and are connecting those IBOutlets in the custom object. In its basic form, you can create a custom cell class and just declare the interface and synthesize the properties and you should be good to go. Just make sure you set up your identifier and custom class correctly in the storyboard.
Hope this helps.
Related
I want to make "tableViewcell.selection = .None" , either by storyboard or code with minimum lines. I have tricks to do it, but I really willing to know if there is any other easiest possible way exist which I may missing in the apple documentation.
I am working with below components:-
UITableViewController
UITableView (Static)
UITableViewCell (no IBOutlet connected because its not required for my simple implementation)
Subviews of UITableView has IBOutlets
In .swift, didSelectForRowAtIndexPath defined because I want user interaction on click (no delegate method called because so far not required)
Is there any way to disable cell selection from storyboard.
Set the selection style of cell to None in storyboard.
Refer this:
https://developer.apple.com/reference/uikit/uitableviewcell/1623221-selectionstyle
How add two images side by side in a UITableViewCell?
The style of UITableViewCell is UITableViewCellStyleSubtitle.
As soon as you start doing anything unusual in a UITableViewCell, it's time to create your own custom subclass of UITableView cell. Trying to muck around with the defaults iOS gives you will only lead to pain and misery.
There are many tutorials and examples out there on the net and it's quite simple to do, e.g.
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
Make sure you pay attention to how everything is wired up in Interface Builder. With XCode 4 you'll also have to create the NIB/XIB on your own in a separate step - see a previous stack overflow response to this here:
How do I create a custom UITableViewCell with nib in Xcode 4?
I'm trying to make use of the new Storyboard designer and the ease of drawing UITableView cells instead of programmatically "designing" each row.
A good example of a standard cell is linked here at stack overflow:
iPhone - dequeueReusableCellWithIdentifier usage
Instead of the linked approach of using "[cell textLabel]" I want to use my own labels, buttons and images on top of each row.
Therefore I created several prototype cells/rows and assigned identifiers to them (using Storyboard).
Now: what is the smartest way of accessing each row's "custom" controls/labels? I tried searching for accessing them by ID, but didn't find anything.
My thinking was it should work along this lines:
[[[cell subviews] getObjectByID:#"labelTime"] setText:#"Whatever"];
Is my expectation of the APIs completely wrong or didn't I just find the right API, yet?
Any ideas or recommendations?
For each new custom UITableViewCell that you create in storyboard, you will want to create a new class file which implements UITableViewCell to link it to. Be sure to map all of the controls within your new cell that you laid out in storyboard to instance properties. Then you'll just use it with dequeuing like normal:
YourTableViewCellClass *cell = (YourTableViewCellClass*)[tableView dequeueReusableCellWithIdentifier:#"YourCellIdentifierStringDefinedInStoryBoard"];
// then set the properties for the class.
cell.labelTime = #"whatever";
There are two ways you can get to your custom subviews. The simpler way is using tags. Every view has a tag property which is an integer. You can set the tag in the nib, and set or get it in code. You can search a view tree for a view with a given tag by sending viewWithTag: to the root of the tree. So, for example, you could give your labelTime view the tag 57, and in your code, you'd find the view like this:
UILabel *label = (UILabel *)[cell viewWithTag:57];
The downside of using tags is that you have to keep the tag numbers in sync between your nib and your code. If they get out of sync, you'll either get the wrong view back or you'll get nil (and since you can send messages to nil, the system won't give you an error when that happens). Still, tags are so convenient that it's pretty common to use them like this.
The other way is to create a custom subclass of UITableViewCell with an IBOutlet property for each custom subview. You can hook up the outlets to the subviews in the nib, and access the subviews via the properties in your code:
MyTableViewCell *myCell = (MyTableViewCell *)cell;
UILabel *label = cell.labelTime;
This entails writing a lot more boilerplate than using tags, but it has the advantage that you will get warnings or errors (either at compile-time or when you first try to load the nib) if your nib and your code get out of sync.
Don't be a doofus like I was being. :)
Make sure you set the Identifier value in the Attributes Inspector of the custom cell. Setting the Restoration ID of the custom cell in the Identity Inspector is the wrong way to go, and that's exactly what I did. Major facepalm for myself.
For more implementation details of how to do it, using the methods described by mservidio and rob, check out the section "Designing Our own Prototype Cells" in this tutorial.
I was working with the grouped table view , and i wanted different controls for every row i.e switch control for 1st,radio button for 2nd ,checkbox for 3rd and so on.. how can this be implemented programmatically that is without using interface builder
thanks in advance
CharlieMezak said is right, you need to create in UIControls directly in cellForRowAtIndexPath , and add as subviews to contentView of the cell
For reference see the link below
http://www.e-string.com/content/custom-uitableviewcells-interface-builder
the link specifies the code to create cells programmatically as well as using IB.
Table View Programming Guide for iOS
Read the programing guide, and remember to use different CellIdentifier for each type of cell.
This is a pretty vague question.
Obviously you need to provide the cells to the tableview in its cellForRowAtIndexPath delegate/datasource method. So, either in that method or during the initialization of your view controller, build the UITableViewCell instances that you need, adding the various controls that you want to them as subviews and connecting the controls to your view controller so you can detect when they have been changed. Then just return the appropriate cell in the cellForRowAtIndexPath method.
Personally, I think it's a lot easier to use IB in cases like this. Just create an IBOutlet instance variable for each custom cell you want, and return the right cell in cellForRowAtIndexPath.
I converted my old UITableViewCell from being programmatically created to using Interface Builder and a Xib. When implemented in code and in edit mode, I moved some of the labels in the cell to make room for the delete button. How do I change the layout of the cell in edit mode when implemented as a Xib? Preferably animated. Links or tutorials are certainly welcome!
If it matters, this is for a 3.0 SDK app.
You need to get a reference to the subviews you would like to move. Two ways to do this are:
Tag the views in IB
Use IBOutlets
If you tag the subview you would like to move, you can find it by:
[cell.contentView viewWithTag:kMyTag];
If you choose to use IBOutlets, you should consider creating Cell Controllers for each cell.
A good tutorial on this can be found here:
http://bill.dudney.net/roller/objc/entry/uitableview_from_a_nib_file
Also consider moving your cell logic into the cell controllers and out of the table view controller as mentioned in this tutorial:
http://cocoawithlove.com/2008/12/heterogeneous-cells-in.html