Static table view cell's height in xib not being applied - ios

I have created a static table cell in a .xib, however when it is displayed the cell height set in the xib is not being applied, instead the cell is being displayed with what looks like the default height for cells.
I have set things up as:
1) The table view controller derives from UITableViewController
2) The TVC is not contained within a xib and is created in code in the app delegate and added to a tab bar controller.
3) The TVC is created using initWithNibName:bundle: with the nib name supplied as the name of the xib containing the table view and the static table cell.
4) The TVC has an IBOutlet iVar of type UITableViewCell*
5) cellForRowAtIndexPath returns this iVar.
6) numberOfRowsInSection returns 1, numberOfSectionsInTableView returns 1;
7) The xib contains a table view, which is connected to the File's Owner's view. The File's Owner is set to my custom table view controller class.
8) The UITableViewCell in the xib is connected to the outlet of type UITableViewCell in the File's Owner.
9) THe Table view style in the xib is set to grouped.
10) The size of the Table View Cell shown in the size inspector is N, and this is where the problem is. If I change N manually or by resizing the cell visual representation then this size is not applied when the table is drawn. I have also tried setting the view Frame Rectangle height in the size inspector to match.
It doesn't matter what value N is, big or small.
11) The cell's height is not being set anywhere in the TVC, if I do set it using self.tableView.rowHeight then its height does change. But I do not want to set it explicitly like this, I want the height size in the xib to be picked up.
This has been driving me nuts for a few days, and reading several tutorials on table views (including Apple's table view programming guide, especially the section on static cells, whose instructions I followed in the first place to set everything up) has not given me any clues where the problem lies.
TIA

Implement heightForRowAtIndexPath and return the height of the UITableViewCell linked to in your IBOutlet.

Do you have - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath defined in your UITableView delegate?
You may want something like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30;
}
Try this post if you're still having trouble try this post: Setting custom UITableViewCells height

I had the same problem and everyone here and on other threads suggests writing code to fix the problem, which seemed wrong. What's the point of using IB if you have to write code to get the properties right?
The answer turns out to be very simple but took a bit to track down. In IB there are two places that row height can be set (just like there are two places to set it in code). One is on the table, the other is on the cell.
The place I was (incorrectly) setting it was the tableViewCell's "Row Height" property in the Property Inspector. Changing this value had no effect on row height at runtime.
Then I found and set the "Row Height" property of the TableView (one step up the object hierarchy) and viola, the row height is correct at runtime. Once this was working I turned off the override of Row Height on the TableViewCell (uncheck "Custom" on the Row Height property). I was afraid this might be evaluated on each cell, which would kill performance since all my cells are the same height.

Related

TableViewCell with SegmentControl

I have tableview in which a particular cell has the UISegmentedControl in it.
My doubt is when a particular segment is selected I need to expand the cell and show the information below and need to hide it when the other segment is selected.
For Ex:
I have three segment A, B and C. When selecting the segment B, I need to show switch below the segment and when the other two segments are selected nothing must be displayed below the segment.
Your dataSource's objects should have an "extended" property.
Each time a segmentedControl is changed, update your dataSource accordingly and reload your cell.
When you configure your cell, change its size depending on the "extended" property of the object.
Theres a few things to do here. You need to make sure your tableview is using automatic dimension for its cell height. Dont use the -heightForRowAtIndexPath method (remove it). Instead set two properties on the tableview :
[self.tableview setEstimatedRowHeight:110];
[self.tableview setRowHeight:UITableViewAutomaticDimension];
This enables proper autolayout (in case you havent already) for each cell.
You will need to have a subclass of UITableViewCell in code, if you havent already (sorry for skipping over some quite big things!). Add the information label to your cell in Interface Builder. Give it a height constraint, and then drag this height constraint into your cell subclass so you can change it.
Now put your code for handling the segmented control in the subclass. When you detect that the central segment has been selected, then affect the height constraint of your information label, like this :
self.labelHeightConstraint.constant = 40;
You may also need a call to setNeedsLayout :
[self setNeedsLayout];
Apologies for the brevity - hopefully this will point you in the right direction. There are some other details - for eg on -prepareForReuse method (on the cell) you can set the height of the info label to 0 (and call setNeedsLayout again).

UITableViewCell size does not follow Content View size

I have two UITableViewControllers which use the same set of cell templates, therefore I have created a table view controller in my Storyboard which contains these templates. My table view controllers instantiate an instance of this template holder tableview, then in -tableView:cellForRowAtIndexPath: they deque one of it's cells, and configure it as they need.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *widgetDict = [self.widgets objectAtIndex:indexPath.row];
DMSWidget *widget = [self.widgetStorageTableViewController.tableView dequeueReusableCellWithIdentifier:#"Key Value Row"];
...
return widget;
}
These cells have their inner layouts set up entirely using AutoLayout.
Everything works fine, except that I get cells which have the standard 44 px height and width of the table view, with Content Views narrower than this (depending which they contain, the ones with less contents are smaller), and are longer than the cells themselves. So the Content Views overlap the next cell but don't fill the entire width.
I don't understand, how this could happen. How is it possible, that the UITableViewCell has different size than it's Content View? They should be the same, isn't it?
What I tried to do but failed:
implement -tableView:heightForRowatIndexPath:: tried to return UITableViewAutomaticDimension, but did not help
reload my table view when the sizes are calculated: does nothing
add hacks to cell implementations, like setting autoresizingMask in -awakeFromNib: I could make the Content View expand to the width of the cell itself, but it was still overlapping to the next cell
UPDATE: It looks like it is not related to the dual table view architecture. When I copy my cell to the tableView where it will show up, has the same issues.
I've found the solution: it was all my fault. In some of the ancestors of my cell has an init, which called -initWithFrame: explicitly with CGRectZero. After removing this, iOS 8 could use it's frame values and everything worked fine.

Static UITableviewCells glitch when UIElements are added

I added a UITableViewController and set the type to static. Everything works when there are no UIElements added to the Cells but when one is added, it completely glitches. I deleted all of the delegate methods in the class but the tableview still is messed up. Any ideas?
Ensure that you've set Custom Row height property.
Select cell
Open Size inspector
Set Row Height to 44 (or any other needed size)

iOS- How do I make each cell in a table view bigger?

This might be a noob question,but how do I make each cell in a UItableview bigger through interface builder?
Select the table view in your nib or storyboard. Then show the Size Inspector. (You can do this by choosing View > Utilities > Show Size Inspector… from the menu bar, among other ways.) The first section of the Size Inspector is the Table View Size, and the first field in the Table View Size section is the Row Height.
Changing the Row Height field in the nib has the same effect as setting the rowHeight property of the table view instance at runtime.
If you want rows to be different heights, you must either implement tableView:heightForRowAtIndexPath: in your table view's delegate, or use a storyboard with static cells.
Assuming you have set the dataSource and delegate to self, this should work:
- (CGFloat)tableView:(UITableView *)tableView heightForCellAtIndex:(NSIndexPath *)path
{
return 50.0f; //Replace with how high you want.
}
Typed on mobile, test before shipping.
Yes, it's code, but it's a whole lot easier than anything else. Easier to change, too.
Select the Table View Cell, then choose the ruler icon on the right panel, check Custom next to Row Hight, then just change the row height.
To change the width select the view controller, find Simulated Metrics in the tab to the left of the ruler, change Size to Freeform, then select the Table View, go back to the ruler tab and you should be able to change the width which the cell will adjust to if Autoresize Subviews is checked (which it is by default).

Grouped UITableView - remove 'Prototype Cells' header in interface builder

I have a UITableView placed on my view controller in IB, using 2 prototype cells and 'Grouped' style. There will only ever be 2 cells in the table, and no group header so I want to resize the height of the table view control to be just tall enough to accomodate the 2 rows (each of height 29) so I have room for my other controls on the VC. Problem is, when I resize the height of the table in IB, it always shrinks at the bottom and retains the 'Prototype Cells' label. This means to get the right height and position I have to lose visibility of my cell designs - see screenshot :
Is there anyway to turn off the 'Prototype Cells' group header label so I can resize my controls WYSIWYG style?
It will be more useful to show your code and what UITableView delegate methods you have implemented in your VC. Anyways, try this : set your header #property for the tableView to nil and if you get no result implement this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return nil;
}
Hope this will work. Cheers
It is possible to change the uitableview to static in stead of dynamic cells. This allows you to add the cells in the layout editor. You can then make iboutlets to the cells and there content. The downside of this is that you will have to use a UITableViewController as root which in turn doesn't let you add anything else then cells, headers and footers.
You can still add the rest of your components in a separate sections and just set the cell to background to invisible. or you could load the controller grab the view and resize it and add it to another viewcontrollers view.

Resources