I have a tableview with UiTableViewCells from a XIB and I want to animate a change of cell height when selected and showing/hiding some elements.
Now, for the animated change of height there's no problems, I've done it with the tutorial in this answer .
The problem is how to toggle some elements!
I can't figure out how to solve this problem! Putting on the xib the elements, they cover the cell above; by code I can't find a method to call when a cell is opened and the animation i finished!
Any ideas?
Thanks to all!
There may be an easier way than this, but one way would be instead of changing the cell height and adding the new components in the animation block, set a flag for that row that it should be expanded then in your animation block call
[tableview reloadData]
And in your
heightForRowAtIndexPath
method return the new cell height and in the
cellForRowAtIndexPath
method of your delegate return the new version of the cell with the additional elements.
Related
So I am messing around with how this feels in my app. I have a UITableView and I have the swiping/panning in place, where I kind of flick the cell away and once the cell is away I have it call a method in the same class where I have my UITableView as a property. Once that cell is swiped away and that method gets called, I want to animate the size of the now empty cell to be about 3 times the size of the cell and have a UIScrollView or UITableView be added. It will be a place to store all comments about that cell.
Basically, I would just like some ideas on how I can replace a swiped cell with a UIScrollView or UITableView and animate it to three times the size of any cell.
In a nutshell, if the text below is tl;dr:
call reloadRowsAtIndexPaths, remember which cell you've changed, update cellForRowAtIndexPath and heightForRowAtIndexPath accordingly, and you are set to go.
Full version:
When you said swiped away, did you meant that you are reloading or deleting certain row of your UITableView?
If so, I think you might be able to do what you described by calling [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; (or whatever direction you'd like), then saving the indexPaths you've changed (in your case only one indexPath), and react appropriately in the cellForRowAtIndexPath dataSource method - changing the normal cell for another one, custom-made, with UITableView or UIScrollView inside, and also reacting in the heightForRowAtIndexPath delegate method, to change the height.
The animation itself will be handled by tableView, so that's an ease for you.
Also, if you will use this method to change the contents of the cell for UITableView, don't forget to assign delegate and dataSource of that cell's new tableView, since that causes lot of troubles when forgot.
I need to expand a UITableView cell when I tap it (animate it as well). However I don't know the cell which is being tapped and I have seen the answer here: Can you animate a height change on a UITableViewCell when selected?
It seems to be a very popular answer, not just in the question. However, I need to get the cell, so how in the heightForRowAtIndexPath method can I get the cell?
Because I have tried this:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
However using this in the heightForRowAtIndexPath method crashes the app as the first time it runs (when the view loads) it gets caught in an infinite loop I think.
Anyway if I was to take this approach how would you suggest i get around this issue? I need to access the cell. Or how else would you expand your tableview cell (animated)?
Carry on adjusting the height as you are.
I would suggest trying to get as much as you can from autoresizing masks as they have worked well for me in the past.
If you need finer grained control you can create a subclass and override the setFrame: method to find out when the frame is changing size and then set up animations appropriately there.
I would like to have a custom UITableViewCell that displays limited data while unselected, but then expand to show more details when selected.
The issue I have is how do I adjust the size of the cell during a select/deselect event?
Make a sub-class of UITableViewCell, and override the drawRect() method.
In your view controller's didSelectRowAtIndexPath, you can switch between the contracted and expanded states. You can change frame sizes, add or remove sub-views, add more data, whatever you need to do.
I was doing it not so long ago. To adjust the size you need to make UITableView call heightForRowAtIndexPath again. It will cause redrawing of cell. Use
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
in order to do that. If you set animation to YES it will nicely animate expanding of your cell. Also to adjust cell, setSelected: in your UITableViewCell subclass may be helpfull.
On selection I change the height of an UITableViewCell (loaded from a nib).
But the separator line is at a wrong position when I do this.
In the screenshot the first row is selected, and therefore bigger than the other ones.
From the separator positions it looks like the cell after the selected cell would be the big one. The second cell "has" exactly the size the first cell should have.
To change the height I save the selected indexpath in tableView:didSelectRowAtIndexPath: and compare it in tableView:heightForRowAtIndexPath:. If the indexpaths are the same I return the increased height. With the help of some NSLog I made sure that the correct height is returned.
And if I would resize the wrong cells the views of the cell would overlap, this doesn't happen.
If I click Line 3 of the first cell the tableView:didSelectRowAtIndexPath: fires and the indexpath is the one for the first cell. So I guess the heights are correct, and the tableview draws the separators on the wrong position.
Does anybody has an idea what I did wrong?
Any solutions? Or should I file another bug with apple?
Edit: If I don't reuse my cells it works as expected.
It turned out that I had switched off "Autoresize subviews" for the UITableViewCell.
If I turn that option on it works as expected.
I had a similar problem. In my case I was using layoutSubviews to do custom layout on my cell. The separator was in the wrong position and the layout of the accessory view was also out.
In my case the issue was failing to call [super layoutSubviews].
This is driving me nuts.
I have a TableView with custom cells. My cell contains a editble textview. Is it possible to change rowheight on cell and textview dynamicly (when I editing the textView) ?
best regards
You can't change the height of a tableview cell without reloading the table. This means that every time a new line is needed in the textview, the tableview needs to reload.
While this can be done (with much manual tweaking,) I don't think the results will make for a good interface. Instead, you should have the cell open a detail view and let the user type there. This is how all the Apple apps handle the same problem and it is the solution most users will expect.
It will also save you a heck of a lot time and frustration.
The row height is calculated from either the delegate or the table view's property. I think the height is determined when the cell comes into view.
One possible avenue is to use - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation.
Is the cell with with the text view unique in the table? You may run into problems if the user is in the middle of editing and the cell needs to be re-layed out and the text view is not unique.
yup!
try resizing your cell and calling
[tableView beginUpdates];
[tableView endUpdates];
every time you need to resize your cell.
that will call heightForRowAtIndexPath: and resize all your cells accordingly
so you want to return the proper height there..
hope that helps:)