estimatedRowHeight API breaks custom cell sometimes - swift

I am using the new iOS 8 API:
self.tableView.estimatedRowHeight = 60
self.tableView.rowHeight = UITableViewAutomaticDimension
This works well in most situations, but it breaks my custom cell if the height of the cell is calculated dynamically sometimes:
Some more information:
It seems tableview does calculate the height correctly(it has empty space between 2 cells), but somehow part of the cell is hidden.
the shown part (the part with white background) of the cell is as height as the cell in my xib file is.
it only happens when the calculated height of the cell is bigger than the hight I set in xib file(which is 60 in my case). If the height is equal to the cell height in xib, it works well. I didn't set any constraint on the height in my xib though.

I saw that you created custom cells via xib files and Interface Builder, correct?
There are settings to use custom heights and set the custom heights in both the cell and table view details in IB. For the cell, I think it's the height property and you check the custom box and input your height.
For the table view, there's a row height property that you check custom for and choose your height there.
I've had wonky stuff happen to my custom cells without making sure these settings are appropriate in IB.
Let me know if that works or if there is another problem!

Related

Collectionview cell with both direction

I have collectionview with horizontal flowlayout direction and size of a UICollectionviewcell is screen size. Is it possible to scroll vertical if content of cell exceeds the screen size height?
Yes you can. I think, iPhone Youtube app is created in the same fashion!! :)
You can create a CollectionView controller with its collection view set to horizontal scrolling, set the collectionView cell size to the screen size. And set the paging enabled property of collection view to
true.
THis will allow you to scroll horizontally as if each cell was a page itself.
Next up, you can add another collection view to each of these cells, with vertical scrolling property of this collection view.
THis can be achieved very easily by subClassing UICollectionViewCell of the CollectionViewController. In the subclass, each cell must have a UICollectionView property.
Remember that the UICollectionView in each cell will have to have delegate and datasource set up properly.
This is exactly what you are looking for.
Source of all information is within this link just above. Hope this helps

UITableView Grouped table border issue

I am creating a grouped table with two sections.In the first section i have 5 cells and in the second section i have two buttons in the lass cell of it.
I have created labels and buttons on each of the cells in the first section and the labels are populated dynamically by the values selected in the previous screen.
Everything works fine as expected,except the borders of the table view gets ruined,it looks like half drawn and incomplete.When i make the table view to scroll up and when its back in the original position,the top borders are spoiled and when i scroll to the bottom the lower borders of the group gets affected making them incomplete.
I am setting the label's and button's attributes in each of cell after initializing them in the viewDidLoad method.
Please suggest me an idea to solve this issue.
Thank you one and all
I am setting the label's and button's attributes in each of cell after
initializing them in the viewDidLoad method.
This is incorrect. You should save the texts for the labels & buttons in a model class & set them in the cellForRowAtIndexPath: method. As, you are not doing this in the aforementioned method, your cell is redrawn when you scroll and the texts are nullified.

UITableViewCells of different heights place their accessoryViews at different X positions

My app has some table cells that vary in height. The cells can also have a UIButton set to be a detail disclosure button (round, blue with arrow) as their accessory view.
Depending on the height of the cell, the accessory view is positioned differently. At first I thought it was my layout code for my cell that was causing the problem, so I set up a quick independent test that uses vanilla UITableCells to remove the possibility that it could be my fault.
I set up a view in interface builder, and just added a view table cells to the view, set their heights to different values and then added a detail disclosure button to each. Nothing more, nothing less.
This is what I see:
UITableViewCells with different x values http://jasarien.com/jing/accessoryView_x_difference.png
I added the size guides (thanks to Xscope) so you can see the difference in the accessory view x positions.
The heights are:
top 37px
mid 68px
bottom 44px (default, untouched height)
If I increase the height any heigher than 68px the accessory view doesn't move any further to the left.
Is this a bug? Is there any way I can prevent this from happening?
Here's the test project to reproduce.
TableViewCellHeightsTest.zip
I got the same problem when I downloaded your file. Instead of setting the detail disclosure buttons manually and assigning them to cells as outlets, delete all disclosure indicators and try setting them this way instead:
Note: I set the background color of the content view to blue for ease of view.
Figure 1 (accessory view height is 17.0)
I was facing this problem and had the luxury of 2 colleagues helping me figure out the cause.
We find out that when using the default UITableViewCell (In my case, of style UITableViewCellStyleDefault though I believe it applies to all other styles), if your accessory view's height is anything above the magic number 16.0, the x position of the accessory view start to differ with the variance of height of the cell.
Figure 2 (accessory view height is 16.0)
My colleague had implemented a custom UITableViewCell and using subviews to layout content and was able to avoid this problem.
So you have 3 options:
Restrict your accessory view's height to 16.0 and below.
Use a custom UITableViewCell and layout your own content as subviews.
Use the default accessory type.
Figure 3 (default accessory type UITableViewCellAccessoryDetailDisclosureButton)
I was having the same issue and fixed it by making the custom accessory view the same height as the cell.

Content of custom table view cells not drawing correctly

I have several parts in my app where I use custom table view cells.
Their content is created with subviews.
The problem is that on some of these cells, the content does not appear at all or does not appear correctly until after the cell was selected for the first time.
One example is a custom cell which has a custom subview which can be set after its creation. This view does not appear at all before I selected the cell and its views were redrawn. Calling -[setNeedsDisplay] in the subview's setter method does not help either.
The problems was that I was using the cells themselves to calculate their height. For some reason, the subviews (which were part of the cell used to calculate the height) weren't appearing correctly in the cells that were used for the actual displaying.
Therefore my advice: Never use a UITableViewCell to calculate its own height. This may work in principle (it doesn't crash), but might bite you later in unexptected and hard-to-debug ways.

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.)