UITableView Scroll Bar changes in size as user scrolls - ios

I have a UITableView where the size of each cell is different (it depends on the amount of content needed to be displayed).
As a result (or at least I think as a result), the vertical scroll bar is initially small when the user first starts scrolling. As the user continues to scroll, the scroll bar grows, reaching its correct length when the user makes it to the last cell.
Does anyone have any ideas on how to fix this (I am using Swift and Xcode 7.1.1)?

That usually happens when the estimated row height you give to the UITableView is always larger than the actual row height.
Since you don't say how you're determining the height of your rows, I'm going to assume you're using auto layout.
You either want to set the estimatedRowHeight to a value that is closer to the average row height.
tableView.estimatedRowHeight = 100
The other option is to use the UITableViewDelegate method to return an estimated height for each row that is closer to the actual value.
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let estimatedHeight = // some fast calculation based on the data for the row
return estimatedHeight
}
The first option is go for if your rows are generally the same size with little variation. The second option is better if to row size can have a lot of variation or you just have no way of knowing what a good average will be.

Related

estimatedRowHeight with multiple lined label causing clipping of cell

Some background : I have a nested tableView in which I can have different types of cell ranging from map to images to key-value pair and more so I had to work with nested tableView which is hard to be achieved by section-row combination
I am using auto-resizing cells.
In my parentTableView I have set
parentTableView.rowHeight = UITableViewAutomaticDimension
parentTableView.estimatedRowHeight = 200
In my childTableView , I have set
childTableView.rowHeight = UITableViewAutomaticDimension
childTableView.estimatedRowHeight = 68.0
Here 68.0 is my cell size which is a custom XIB having two UILabel in vertical sort of orientation. Now when the labels are not multiple line, the tableView is appearing fine . But when i add multiple lines to the labels, the cells are getting clipped
But when I increase the estimatedRowHeight to say a greater value like 80, it is not getting clipped but displaying the content AND add some extra white space at the end
What I believe is happening is that it looks at the estimatedRowHeight value and number of cells that are to be displayed and calculates the parent cell size as per that . And when multiple lined label occurs it increases the size of the cell but the parent cell size has already been defined so it gets clipped
Can anyone please help me out i have tried a lot of fix but nothing is working.
First of all, I would like to suggest that please avoid the nested tableView concept. You may facing glitch issue while reloading it and when data will be huge, it may cause UI blockage.
Coming to Solution,
Please try the following solution:
- Add constraints to each element as per your requirement except last UILabel.
- Add top and bottom constraints (compulsory) with any other constraints (if you need). It will help the cell to understand the run time height of that cell.
- In tableViewCell(_ heightforrow) method, put the height as return
UITableViewAutomaticDimension
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}

How to get the height of UITableView that is bigger than the screen (iOS)

I have a tableView that is larger than the screen height, and has dynamically sized cells (UITableViewAutomaticDimension). I'm getting the height of the tableview the following way:
self.tableView.reloadData()
self.tableView.layoutIfNeeded()
let tableHeight = self.tableView.contentSize.height
However, this height isnt always correct, especially for long lists with cells of drastically different sizes. It seems to just take the table's estimated height and multiplies it by the number of rows.
How do you get the exact height of a tableView that has different cells?
i had to create a method to detect when the bottom of the tableview was hit and reload the tableview, it was the only solution.
rule of thumb, try not to nest dynamically sized tableviews inside a scrollview.

Xcode image occupying whole tableView instead of cell

So I am creating a prototype which I will use later.
I have a table view with one cell, then I will make the whole table populate several cells (that is working)
My problem is, the image on my cell, instead of occupying the cell to occupies the whole table view as follows
Any ideas?
Thanks
Storyboard
Simulator
EDIT
repo: https://bitbucket.org/eduardoreecreate/coderswag-ios
Try setting the image view's content type to AspectFit and ClipToBounds to true
By default, an image view under the influence of auto layout wants to be the size of the image. Your image is big. Therefore the image view is big. Therefore the cell itself is big, because you are autosizing the cell height to match the height of its contents.
The simple solution is: don't do that! Before you put the image into the cell, munge it in code so that it is the correct height. It is very foolish to put a huge image into a small table cell in any case, or any small image view, because you're still wasting all that memory. Always try to crop / shrink the image before putting it into the interface.
More elaborately, you can give the image view an absolute height constraint to keep it from growing beyond a certain height. Or, as you've already been told, don't use automatic cell height; set the cell height to some absolute value by implementing heightForRow.
Implement heightForRowAt UITableView's delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
// Customize or write a logic as per your requirement like for 1/4 of screen UIScreen.main.bounds.size.height / 4
}
If you select the cell in IB, then the Size Inspector shows Row Height at the top (with a "custom" checkbox), but if you select the whole table view the Size Inspector shows Row Height at the top there too (no "custom" in this case). The table view setting is used for dynamic cells. (Not sure if it's intentional)

In tableview i have 4 static cells. How do i make row height automatically calculated to fit the label inside and one manually

So i have four sections and one cell in each one of the sections in my static tableview. One of them is displaying a picture and for that one i manually calculate the ration and multiply it by the width and that way i get the correct height. I do that using the "heightForRowAtIndexPath".
I also have three other cells and each one has a label in it. The labels content is different each time since i'm segueing to it from a different cell so sometimes the cell should be big enough for 1 row of text and sometimes for more. How do i calculate/set that to happen automatically?
Also in my storyboard i have the constrains for the label set to be 8 point away from the right left and top, thats all the constrains that are on the label. I already tried setting the tableView.rowHeight to UITableViewAutomaticDimensions but that doesn't do anything. The rows just stay at the same height as they were set in the "heightForRowAtIndexPath" or the same as in the storyboard if that function is not implemented. Been trying to solve that for probably more then an hour now and still cant figure it out. Thanks for the help. Also i'm doing all of this in swift.
If you implement heightForRowAtIndexPath it will also override any value for the table view's rowHeight. So, in heightForRowAtIndexPath return UITableViewAutomaticDimension, and do the same in estimatedHeightForRowAtIndexPath too.
As long as your cell has a solid Auto Layout configuration, that's all it takes to make auto-sizing cells work.

UITableViewCell's rectForSection returns estimate when section not visible

I'm currently trying to add infinite scrolling to a UITableView, which contains a number of calendar events. Since the event title doesn't always fit inside a single line I've added a multi-line UILabel to the cell. In order to calculate the height of the cell I'm taking advantage of UITableView's new self-sizing cells via Auto Layout in iOS 8. WWDC Session 226 talks about this in more detail.
In order to implement the infinite scrolling mechanism I'm overriding layoutSubviews where I need to calculate the height of a section which at the time given ist not visible on screen. This can be done by using [self rectForSection:0]. When doing so the table returns a height based on the estimated row size I had to define inside the table's initialiser in order to make self-sizing cells work.
self.estimatedRowHeight = 44.0;
self.rowHeight = UITableViewAutomaticDimension;
When the section gets on screen I get the correct size of the section, but since I have to update the table's contentOffset based on the computed height of that specific section this causes my table to jump up and down.
Any ideas on how to solve this? Is there a way to force a section to return the actual height and not the estimated one?

Resources