Stop TableView / TableViewCell animation (probably on willdisplaycell) - ios

I've noticed this with a couple of UITableView's that I have coded, and that is when there is a lengthy list, or if I animate the displaying of the table itself, the cells have an animation, like the table is constructing the cells as I'm scrolling down.
The animation looks like the accessory view sliding from the left to the right, and I have a top-right label that does the same.
On another table that I slide in from the bottom, it looks like the buttons and text from the table expand as the table does.
How do I stop this?
I am using Swift, but if you know the answer in OBJ-C that would be okay too.
Also, if you need a preview, check this:
I've tried searching for this and I only get posts about making an animation within the table cells not removing this one!

So I found out how to stop it! It was because of the accessory view and making sure the cells have performed the layoutIfNeeded.
First, I removed the accessoryview from StoryBoard and in willDisplayCell: I used this:
UIView.performWithoutAnimation { () -> Void in
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.layoutIfNeeded()
}
As well as:
UIView.performWithoutAnimation { () -> Void in
cell.layoutIfNeeded()
}
in cellForRowAtIndexPath:

Related

Animation in UITableViewCell breaks after cell deallocation

I have an expanding view in the cell. After i press show more button it works fine. But when i scroll down and that cell deallocates from memory and then i tap on show more button again animation breaks. How can i fix it?
Project repo on Github
Collapsed view:
Expanded view:
Debugger view, before scroll:
After cell deallocation (after scroll) breaks like this, expands behind second cell:
Based on your code and comment...
Instead of expanding your cell, you're setting clipsToBounds = false and expanding the cell's contents, allowing them to extend outside the bounds of the cell.
As you scroll a UITableView, cells are - of course - added / removed as needed. But when they are re-added, the "z-order" changes (because, with "standard" table view usage, it doesn't matter).
So, after scrolling down and then back up, and then tapping the "Show more" button, that cell may be (and usually is) at a lower z-order ... and thus its expanded "out-of-bounds" content is hidden behind the cell(s) below it.
If you want to stick with that approach (as opposed to expanding the cell itself), you can try implementing scrollViewDidScroll to make sure that cell is "on the top":
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// make sure the first row is visible (not scrolled off the top of the view)
let pth = IndexPath(row: 0, section: 0)
if let a = tableView.indexPathsForVisibleRows,
a.contains(pth),
let c = tableView.cellForRow(at: pth) {
// bring it to the front -- i.e. the top of the z-order
tableView.bringSubviewToFront(c)
}
}

How to animate UIStackView hiding inside UITableViewCell properly?

I want to animate the subviews of the TableViewCell which is StackView. When I hide the StackView, the TableViewCell height not updating. After googling, I found that I should call tableView.beginUpdates and tableView.endUpdates to notify tableView that there is a change in the cell. The problem is the hide animation and the change of tableview not sync.
Here is the view hierarchy for tableview cell
Content view - Container View (for card shadow) - Container Stack View - [Stack View for label and switch] & [StudentStackView for container of StudentView]
How can I sync the cell height and hide animation the correct way?
Here is the github repo: GitHub
Gif of the App:
You are right in using beginUpdates()/endUpdates(). Make sure you're not placing the someArrangedSubview.isHidden = true/false in an animate block since the table view and stack view will handle the animations accordingly. When the table view begins update operations, the stack view will resize any arranged subviews that you aren't removing to fill the entire space of the cell (even if you have height constraints on the arranged subview). In my case, the cell content jumped every time I wanted to collapse a cell via removing an arranged subview--so I added a dummy view between the view I wished to remain static* and the collapsible view. The static view won't resize, and the dummy view will expand/collapse as needed. Hope this helps.
*static in the sense that I didn't want the view to move when animating.
`public func setup(classRoom: ClassRoom, toggleInProcess: #escaping () -> (), toggled: #escaping () -> ()) {
containerStackView.addArrangedSubview(studentStackView)
self.nameLabel.text = classRoom.name
self.activeSwitch.isOn = classRoom.isActive
self.studentStackView.isHidden = !self.activeSwitch.isOn // Let him know his hide/unhide.
for student in classRoom.students {
let studentView = StudentView()
studentView.nameLabel.text = student.name
studentStackView.addArrangedSubview(studentView)
}
activeSwitch.addTarget(self, action: #selector(toggleShowStudents(show:)), for: .valueChanged)
self.toggleInProcess = toggleInProcess
self.toggled = toggled
setupShadow()
}`
` #objc func toggleShowStudents(show: Bool) {
UIView.animate(withDuration: 0.3, animations: {
self.studentStackView.isHidden = !self.activeSwitch.isOn
self.toggleInProcess()
self.containerView.layoutIfNeeded()
}) { _ in
self.toggled()
}
}`
your studentStackView also know his hide/unhide status while assigning values in function setup.
I left this as a comment but for anyone else experiencing this behavior, the root cause is the UILabel is expanding to fill the visible area before collapsing.
This can be fixed by doing the following 2 things:
Right below the UILabel, insert a Blank UIView
Adjust the Content Hugging Priority of the UILabel to "Required"
With these two adjustments, instead of the UILabel expanding to fill the visible area, the UIView expands instead. Visually, this appears as if the the cell just collapses.
tableView.beginUpdates and tableView.endUpdates are functions that should be called when you are about to modify rowcount or selected state of the rows.
You should try reloadData or reloadrowsatindexpaths, that should take care of the cell height adjustment.
You would better do it using performSelector API so as not to cause recursion in cellForRowAt call stack.

there is something like didAppear for a cell in a collection view?

I have a problem with my textViews, because when they appear, always appear scrolled to the bottom, I read here that is a problem that occurs when the textview has constraints, and the solution is set the isScrollEnabled to false and in the didAppear set to true, but because the superview of my textview is a cell in a collectionView, I don't know how to solve this
BTW, I'm using swift 3 and XCode 8
edit: Sorry, I'm not using a nib
I suggest to override prepareForReuse to make any changes before the cell will be visible.
I just solved this, but i did a lot of things, i'm new in ios programming and i don't understand very well a lot of things, but this is how i did it.
To make a textView scrolls to top in a collectionView in my view controller i search for any textViews there and when find it, i set the contentOffset, for the first cell i used the viewdidlayoutsubviews, but for the other cell i had to use the willDisplay and didEndDisplaying, making exactly the same:
let container = collectionView.subviews[0].subviews
for v in container {
if let fView = v as? UITextView {
fView.contentOffset = CGPoint(x: 0,y :0)
}
}
I enter to the cell subviews directly, and set is contentOffset, but the trick is to reload the cell in the cellForItemAt, just like this:
collectionView.reloadItems(at: [indexPath])
and thats it, i hope this helps some one and thanks for your answers.

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!

When I page to the right using UIScrollview within my UICollectionViewCell, it also scrolls in every fourth cell from that cell, up and down

When I page to the right using UIScrollview within my UITableViewCell, it also scrolls in every fourth cell from that cell, up and down.
It is creating the scroll view based on a property within the containing view, contained by a UITableViewCell.
the only method the scrollview has is
-(void)scrollViewDidEndDecelerating(UIScollView *)scrollView{
scrollview.contentoffset = CGPointMake(scrollView.bounds.size.width,0);
}
Basically every time a new cell is drawn, it takes the scroll position of the cell that has just been undrawn.
Is there any work around for this besides disabling deque? I mean, I'd like to be able to scroll down and then scroll back up without affecting the horizontal scroll within the cell.
Thanks for the help
Sounds like your cells, which are reused/dequeued, aren't being reset.
When you dequeue and configure your cells you need to include logic to set as well as reset, something like this:
if (isCustom) {
[cell setupAsCustom];
} else {
[cell setupAsDefault];
}
As opposed to just this:
if (isCustom) {
[cell setupAsCustom];
}
In this case the customness of the cell is its contentOffset. Hope this helps.

Resources