Dynamically updating cell height without having to scroll Swift - ios

By default the height of my cell is set to 140.
But if expanded, it should be set to 265.
This is what I have:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if(expanded){
return 265.0
}else{
return 140.0
}
}
The problem is, I need to scroll down, and scroll back up for the cell to change height. How do I fix this?
Secondary question (more interested in the above question, just if anyone happens to know)
Is it possible to have the cell animate from height 140 to 165?
Thanks

You need to reload the table view after you change anything in it like so:
tableView.reloadData()
When you scroll away from the cell, if it goes off the screen it is unloaded. When you scroll back up, it reloads it. You are basically just reloading the data as scrolling would do.

This code should work here:
tableView.reloadData()
It will reload the table everytime you changes something.
Source

Related

Tableview cell does not cover the whole area as expected

I've been googling an answer for this for hours but cannot find anything that actually works in my case and I've no idea why.
I'm creating a tableview which should be from the top of the tableview to the bottom of the tableview, but in this case it doesn't.
img here
In this image you can see how it looks like in the storyboard (Which is also the way I want it to look like), you can see it almost cover the whole tableview.
And this is the result:
result of tableview
I'm trying to make the tableview cell start from the top and contain all the way to the bottom of the tableview (Well, like the storyboard image)
I've added this...
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 410
}
You dont need to use UITableView
You add vertical scrollview and add one contianer UIView in it then you can add mutiple UILable and UITextField one bellow another.

Expandable UItableview content height are different during expanded or collapsed

I have created an expandable tableview by separated with multiple sections. Because this tableview is inside a container view, so I need to update the container view size every time the tableview expand or collapse.
I update the container size with tableview.contentsize.height but I found out the content size are different when its expanded or collapsed.
For e.g, lets say there are 4 sections, if I collapse the 3rd and 4th sections the tableview will return me height of 200. Meanwhile if I expand the table just 1st and 2nd section while 3rd and 4th is collapsed, the content size height is more than 200.
I find it very frustrated why the program is giving me such confusing information, because I need a proper content size height to update the container view. Anyone here can enlighten me?
Instead of using the contentSize try calculating the height on your own. It'll be much easier if every cell is the same height. Let's say they are all 50 height and there is 1 row in section 1 and 3 rows in section 2. (1 + 3) * 50 = container height. Obviously if you have sections and insets you will also need to include that into your calculation.
If your cells are different heights, just call the table views cell height delegate to get the relevant height.
If this doesn't work for you then please be more descriptive and add some code.
The behavior of content size that you've described is correct. If you want to know the height of the UITableView you should look at it's frame, not at it's content size.
I'm not sure why you need a container view for the table view, you can layout it like the simple UIView, then you'll have one less problem, just change the tableView frame.
Update:
After reading comments i see that you want tableView inside of tableViewCell. Most part you'll have to do yourself, but i can help you with UITableViewAutomaticDimension.
Try adding this to your viewController:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAt indexPath: NSIndexPath) -> CGFloat {
return 44//approximate cell height
}
The main thing is to set constraints properly in interface builder. And remember that constraints won't change the height of the cell on builder, you may have to do it yourself in the Size inspector. Anyway if you won't - it will be resized during the build.

UITableViewCell height from initial size to full screen

how i can swipe down (scroll) one cell from my tableviewcell (static cells) and make this cell fullscreen?
I know how make the cell full height:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return tableView.frame.size.height;
}
But how i can start this cell with a fixed height and after swipe down (scroll) the cell, make the cell fullscreen?
Thank you
The way I do this sort of thing is actually to treat this as a master-detail interface. The gesture does not really expand the cell; it does a "push" of another view controller containing the full-screen version. And it does it with a custom transition animation, so that it looks like the cell is expanding to form the full-screen version.
And the same thing when returning (popping), in reverse.
This is an example; it isn't 100% identical to what you're doing, but it shows how the push-plus-custom-animation can give the sort of effect you're after:
You can also monitor frame changes in your cell class and expand when the cell is pressed, (not scroll if that's what you really want) and by monitoring frame change you can expand the cells height. Let me know if you want more information because this is on selection, not on scroll.

How to reload tableview after reordering a row? Rows have variable height

I let user to reorder rows in tableView. Because this event affects content - some numeric values in cells should be updated - in all the other rows, I call a reloadData in moveRowAtIndexPath. And then strange effects occur.
I.e. cells seems overlapping when touching dragger, and some cell starts to move up and down. Its important to know, that cells height are varying.
The strange, that if I remove reloadData from moveRowAtIndexPath, then all these phenomenons disappear. Only the content is invalid.
So how should I reload data after reordering?
UPDATE: What I have done meantime reconfiguring cells in viewDidLayoutSubviews instead of call reloadData end of the moveRowAtIndexPath. And it works 90% like I expect, but still rows are sometimes somewhat higher they should.
override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
//..
reorderOccured = true
}
override func viewDidLayoutSubviews() {
if reorderOccured {
for cell in tableView.visibleCells() as! [UITableViewCell] {
let ip = tableView.indexPathForCell(cell)
if ip != nil {
self.configureCell(cell, indexPath: ip!)
}
}
reorderOccured = false
}
}
You shouldn't call reloadData after reorder.
You have to make the same changes to your data, as the changes that has been made on the screen.
For example: if you moved cell nr 2 to position 6, you have to remove your object that populate cell nr.2 and insert it again at position 6.
You didn't provide enough details, but usually you would keep your data in an array of objects. This array you have to make the changes to, so your backing datasource is valid.
Here's a link to details from Apple.
I just read the updated after I posted my answer. It seems like you really need to reloadData. In this case I advise to reload after a small delay with a dispatch_async block on the main thread. Say after 0.1.
I found here the answer: https://stackoverflow.com/a/4692563/239219
[tableView beginUpdates];
[tableView endUpdates];
This code forces UITableView to reload cell sizes only, but not cell contents.
I can't yet comment on pteofil's answer, but he is correct: if you have a numbered set of rows in a table, and you move one calling moveRow(...), your animation for the move will be cancelled by a tableView.reloadData().
As such I delayed the data reload (which renumbers all the visible cells based on the updated data source (don't forget to do that when you move stuff around!)) a couple hundred milliseconds and it works perfectly now and looks great too.

How to make UITableView appear from bottom to top when scroll?

I'm looking just for general scenario.The idea is when app launches user can see only first cell of UITableView at the bottom of UIViewController. When user scrolls up full table appears and when scrolls down only first cell is displayed again. Something similar like keyboard in Facebook messenger app, but with tableView. For now I added tableView as subview to scrollView, but problem is tableView appears from top to bottom, and I'm looking for solution how to make this work upside down.. So, tableView have to appear from bottom to top of UIViewController.
My idea would be:
Give your UITableView the desired frame at viewDiDLoad (probably the height of 1 cell, at the bottom of your UIView)
Let your UIViewController implement UIScrollViewDelegate
At - (void)scrollViewDidScroll:(UIScrollView *)scrollView of UIScrollViewDelegate check which element is scrolling (if its your UITableView) and also check which direction user is scrolling
Change the frame of the UITableView as you wish, you will also have to come up with some logic to block further changing of the UITableView's frame (a BOOL would do good here I guess)
Not sure I understand your correctly but, maybe, this will be helpful
I believe you can try to add zero cell (or first section header view of your UITableView) with transparent background. So that your first cell will be placed on the bottom of screen and UITableView height will be equal to screen height.
In this case, you will have only one scrolling view (UITableView, in particularly). Following method can be used to perform expandable animation and scroll table to top to hide zero cell when user taps on first cell:
[UITableView scrollRectToVisible:animated:]
After that you can leave UITableView as it is and "constrict" your table whenever you need in the same way as it was expanded before.
I understand what you want to do.
In general, UITableView shows cells from top to bottom.
You can add transform in tableview:
tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
Then your tableview will show the cells from bottom to top and you can scroll tableview from bottom to top.
But cells will be transformed as tableview, so you have to add same transform to cells also.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")
**cell.transform = tableView.transform**
return cell
}
All done. Hope to helpful!

Resources