Custom UITableViewCell with Storyboard - iphone

My goal is to create a custom UITableViewCell which contains 2 UILabels. One is the title and one is the detail. The detail label should allow for 3 rows of text.
So I went on and created a custom table view cell in storyboard. I also created a subclass of UITableViewCell and linked the two together.
I the added two UILabel to the cell in storyboard and placed them where i wanted them to be and linked them to their coresponding outlets in teh subclass. Since the content of the labels varies I wanted to align the text vertically to the top. As I understand the only way to do this is by calling the sizeToFit method on the label. I execute this under in the sub class of UITableViewCell:
-(void)layoutSubviews {
[super layoutSubviews];
[self.detailTextLabel sizeToFit];
}
So far everything seems fine and the text in the detailTextLabel is aligned as it should. Although when i satrt interacting with the cell, for example slide my finger over it so the delete button appears, the detailTextLabel will change size to the size that was set in storyboard. This causes the text to be misaligned. Similar things happen when i select the cell and change to another view and the return to the table view via a tab bar
My question is: Is there any way of creating this custom cell differently using storyboard or is my only alterative to create everything programtically?
Regard, Christian

Maybe you should take a look at this if you still want to vertically align your text in your UILabel without sizeToFit (who will resize it when you will interact with your cell).
About your question, I think you can create your custom cell from a xib file like this.

Related

iOS 5 Create a custom view

I was wondering what is the best approach in creating a view such as the one on the left hand side of the following link?
I know how to make apps that use the templates in the storyboard but kind of lost on the guideline when it comes to creating custom views. Does it all get created at runtime using code? Do I need to add one subview per section (Gift, Birthday,...) to my main view? any general approach for creating such a view is appreciated.
At the top level it looks like they have a UIToolbar and a UITableView or a UIScrollView. They have used some custom graphics in the toolbar; possibly they have implemented their own toolbar class.
The table view is probably using a custom UITableViewCell subclass. It is using variable height rows (by defining the tableView:heightForRowAtIndexPath: method in the delegate). They might just be using a scroll view instead of a table view, but I'll assume it's a scroll view for this discussion.
Each cell has appears to have at least three subviews: one to draw the cell's title bar, one to draw the cell's contents, and a UIPageControl to draw the page dots under the contents.
The contents part of the cell looks like it's probably a scroll view, with some subviews to draw images (UIImageView) and labels (UILabel). The subviews of the scroll view are quite different for the different table view cells.
You could lay out an interface like this using nibs. I'd probably use four nibs: one for the top-level with the toolbar and the table view, one nib for the table view cell, one nib for the gift layout (which has a UILabel over a UIImageView), and one nib for the person/date
layout (which has a UIImageView to the left of three UILabels).
You need to set some properties in code. For example, suppose you have one nib for the overall layout of a table view cell. It probably has a view hierarchy like this:
UITableViewCell (or subclass)
UIView to provide the colored stripe across the top
UIImageView for the icon
UILabel for the title (“Browse Gifts”, “Birthdays”, etc.)
UIButton for the disclosure indicator
UIView to provide the white background with shadow
UIScrollView to hold the main content of the cell
UIPageControl
When you load this nib to use for your “Browse Gifts” cell, you need to set the shadow properties of the white background view's layer, because you can't do that in the nib. You need to set the color of the stripe, the icon, and the title text of the cell on the appropriate views. You need to add content to the scroll view (which probably involves loading another nib once for each content item). You need to set the number of pages on the page control.

Custom cell in UITableView

I am making a custom cell which has text fields and as a result the custom cell is greater than the size of the iphone screen.But i am not being able to do horizontal scroll to reach the end of the cell.
I have tried using viewController and adding table view to it as well as creating table view controller and adding custom cell to it.
when doing it through TableViewController,I am not able to horizontally scroll it,whereas i am not getting how to add Custom cell class to the tableView object placed in viewController.I know that UITableView is a subclass of UIScrollView but not able to implement it.Please help.
Thanks
as per the documentation at
A table view in the UIKit framework is limited to a single column
because it is designed for a device with a small screen. UITableView
is a subclass of UIScrollView, which allows users to scroll through
the table, although UITableView allows vertical scrolling only.
so as desired in your case you will have to take some other approach. Now using Custom cell with TextFields should not actually need you to require Horizonatl scrolling. If the text is really large you would like to consider using TextView instead of TextFields.
Hope it helps
Ideally, you should be truncating your text. But if it is important for you to display the entire text, add a scroll view as a subview (or set it as the content view) and add your labels to the subview. Each cell will then be individually horizontally scrollable.

Custom UITableViewCell for a grouped table view

I have a grouped table view.
I'm trying to create a custom cell using interface builder, however when I'm putting a label, or a view as a subview for the cell, and stretching it to the entire cell, when I run the app the label goes beyond the cell's dimensions.
Any reason why it would do that ?
I tried to play with the resizing mask to no avail.
When the table is plain, there's no problem.
I guess I'm doing something wrong, cause it's not suppose to be that complicated.
I don't have much experience in creating UITableViewCells with IB, but I would recommend adding your subviews programmatically using a UITableViewCell subclass. Your issue is that the subviews are added directly to the UITableViewCell view, and the left and right margin are part of that view.
The UITableViewCell subview that represents the actual "active" space (the white part of the cell) is contentView. If you add a subview to contentView, then it shouldn't appear outside it. In other words, CGPointZero of contentView is the top left point of the white space.

Adding a subview to UITableCellView

I want to add a subview to the UITableCellView class. However, non of the provided views in the class seem to be able to do exactly what I was looking for.
I basically want to add my own background view, filling the whole cell. However, if I replace the backgroundView, the style from the grouped table view layout isn't displayed anymore. If I add a subview to backgroundView, the subview is not shown at all. If I add a subview to the contentView, I can't draw behind the accessory icon.
What am I missing?
Basically you can't change the backgorund of GroupedTable View.
Try using it with PlainTable.
and add the your backgroung image (of size = cellsize) to cellforRowAtIndex method.
You might want to take a look at this article:
"Easy custom UITableView Drawing"
In particular:
First: the UITableView does not itself
draw anything except the background.
To customize the background of a
UITableView, all you need to do is set
its backgroundColor to [UIColor
clearColor] and you can draw your own
background in a view behind the
UITableView.
Simply add the custom view as part of your contentView. Set a unique reuse identifier for that cell, configure it when you create it and from then on simply reset the data components (this is easiest to do if you create a custom cell controller class so that it can track all the parts and use setters/getters for the data).

Can a UITableView (not UITableViewCell) have variable size?

I have a UIView (created in IB) with a grouped UITableView as a subview. Below this table view is a UIButton. The XIB containing the view will be loaded by a few different viewcontrollers, and so the contents of the table view can vary between one and four cells.
Here's what I want to achieve: when the view loads, the height of the tableview (tableView.frame.size.height) should be adjusted depending on the number of cells, and the button should be placed just beneath the table view.
Can this be done? Could it somehow be done if the view is created programmatically?
Thanks in advance
Edit: Pxl's suggestion was just what I was looking for. A while later, the need arose to have more than just a button below the table view - this was accomplished by creating a separate view containing everything I needed, and implementing the tableView:viewForFooterInSection: and tableView:heightForFooterInSection: functions.
A note for those of you trying to do the same thing: the tableview has to be programmatically created if you want different heights for the footers, or footers for only some of the sections. This is because the footer height set in IB will override the one returned from the tableView:heightForFooterInSection: function.
if there are only a handful of rows, may i suggest that you create a special UITableViewCell that contains just a button?
then make that button cell the bottom row of the last group all the time. make the group so that it will be unlabeled and appear as if the button is sitting at the bottom of your tableview. this way you won't have to muck around with recalculating the tableview's frame and redrawing it.
if the tableview will scroll due to there being many rows, then you'd be calculating the height of the tableview up to a set max (at which point the tableview will need to scroll to show more rows).
once you've determined the height of the tableview you'll need to display your rows, make a frame of the appropriate size, set the tableview's frame to it, position the button just under the tableview, and then redraw the view.
the layout and positioning in this case will need to be done programmatically.
UITableview is a subclass of UIView, so you can change its frame to suit your needs just like a UIView, and UITableView will manage drawing itself to whatever frame you give it.
Just use the methods UITableViewDataSource and UITableViewDelegate provides you.
height = [self tableView:numberOfRowsInSection]*[self tableView:heightForRowAtIndexPath:] + A_CONSTANT_FOR_HEADER_AND_FOOTER_HEIGHT
I agree with pxl that adding a cell with the button in it may be the easiest way to accomplish what you want.
Whether or not you do that, the table view's sizeToFit method should resize the view to (just) fit its contents. If that doesn't work, you can use numberOfSections and rectForSection: to build a loop that determines the height of the table's contents, and reset its frame manually. (And then position the button underneath.)