Visible UITableViewCells not resizing on rotate - ios

I have a UITableView with custom cells. When I rotate the device, the visible cells do not resize. Scrolling to new cells works fine and when I scroll back, all is well, but is there a way to get the initial, visible cells to smoothly resize on rotate?
[self.tableview reloadData] from the UITableViewController; works, but I don't think that is the optimal solution, as it requires a network call for data retrieval.
I am already calling the following code from the UITableViewController in order to layout some gradient layers I use, but this does nothing to resize the ImageView.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
for (ProjectCell *cell in [[self.tableView.subviews firstObject] subviews]) {
cell.laidOut = NO;
[cell layoutSubviews];
}
}
Thanks

I believe this will do what you want:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
As long as the cells you want to reload are the visible cells.

Related

Asynchronous image download inside a cell without knowing the height

I'm facing this problem and I've been trying to figure out how to fix it but without success.
I have a table view with cells that contain an image which I still don't know which height is going to be. I created an outlet to the height constraint of the imageView and I'm downloading the image asynchronous with PinRemoteImage (I can use SDWebImage too but I think it's buggy in iOS 9). Inside the blocks is where I assign the new constant for the height constraint and then I do a layout update.
The cell never updates, the only way I can see the image correctly is scrolling down and then up (when the tableview repaints the cell)
__weak typeof(UITableView*) weakTable = tableView;
__weak typeof(NSIndexPath*) index = indexPath;
[cell.commentImageView pin_setImageFromURL:[[CTImageHelper sharedInstance] resizedImageURLConverterFromStringWithPrefix:comment.image.completePath andOptions:optionsString] completion:^(PINRemoteImageManagerResult *result) {
CommentTableViewCell *cellToUpdate = [weakTable cellForRowAtIndexPath:index];
cellToUpdate.heightSize = [NSNumber numberWithFloat:result.image.size.height/2];
cellToUpdate.commentImageViewHeightConstraint.constant = result.image.size.height/2;
[cellToUpdate setNeedsLayout];
}];
This is the code for setting the table view row height automatically
self.postTableView.estimatedRowHeight = 244.0;
self.postTableView.rowHeight = UITableViewAutomaticDimension;
And an image about the cell constraints:
Any ideas about what am I doing wrong? Thanks!
Put layoutIfNeeded just after setNeedsLayout
[cellToUpdate setNeedsLayout];
[cellToUpdate layoutIfNeeded];
// and then tell the tableView to update.
[weakTable beginUpdates];
[weakTable endUpdates];
// then scroll to the current indexPath
[weakTable scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionNone
animated:NO];
Update [weakTable beginUpdates];[weakTable endUpdates]; might crash if call it more than once at a time. you need to make sure there is no colision between them.
You may also try just reloading the cell itself.
[cellToUpdate setNeedsLayout];
[cellToUpdate layoutIfNeeded];
[weakTable reloadRowsAtIndexPaths:[indexPath] withRowAnimation:UITableViewRowAnimationNone];
You should call layoutIfNeeded after calling setNeedsLayout to actually trigger the layout.
You need to tell the tableview that the height has changed on it's cells by triggering an empty update, apart from the setNeedsLayout and layoutIfNeeded:
[tableView beginUpdates];
[tableView endUpdates];

Insert cell at indexPath, not working correctly

I have a custom UITableViewCell with objects in it (such as UILabel, UITextView, UITextField etc.). When a button gets selected, a cell gets added to the tableView.
When I run it on the simulator, and the cell gets added, all the visible cell's and subviews height get really compact. (I do have auto constraint applied.)
....
[[self myTableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
If I do the following, the cells get back to normal:
NSArray* visibleCellIndex = self.myTableView.indexPathsForVisibleItems;
[self.myTableView reloadRowsAtIndexPaths:visibleCellIndex withRowAnimation:UITableViewRowAnimationFade];
[self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
The problem with reloading the visible cells, is: First, that's a workaround, not getting to the source of the problem. Second, it's not a fully functioning workaround, because the whole tableView scrolls all the way up for a second, then scroll back to position.
The reason why it was shrinking, is because, you have to implement the method of heightForRowAtIndexPath.
The only problem now, is that the tableView jumps up, then scrolls to position. Don't know why.
Does your target run only on iOS 8 and later? If yes, you can set self.tableView.rowHeight = UITableViewAutomaticDimension to enable Autolayout for your cells. Then, you also don't need to implement delegate tableView:heightForRowAtIndexPath:.
If you're already doing this, your problem probably lies in your custom cell. Maybe its constraints are not well defined? How do you initialize the cell's constraints?
Another idea is to trigger the layout pass manually in tableView:cellForRowAtIndexPath:. After the cell has been initialized and its text label values have been set, call:
[cell setNeedsLayout];
[cell layoutIfNeeded];

Change in TableViewCell to take effect immediately

I have a TableView with a custom cell. One of the subviews in the cell is a UIButton. When a user clicks on the button, I want the background to change. I get all of that working. But the problem is I cannot see the change until after I scroll the affected cell off screen and then return it on screen. But I want to see the change immediately, without the onscreen offscreen bit. How might I do that?
For a bit more about my implementation:
Inside the method (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I have the line
....
[cell.myBtn addTarget:self action:#selector(onMyBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
And then inside the onMyBtnTapped method is where I effect the color change.
So perhaps what I need to do is to redraw a specific cell from the parent view controller (?).
a bit more
I have gotten as far as getting the cell using [self.tableView cellForRowAtIndexPath:indexPath];. But now that I have the cell, I don't know how to get it to redraw itself. I do this on android all the time. I am not sure how to do it on iOS.
You can reload the UITableViewCell on button click:
[self.tableView reloadRowsAtIndexPaths:#[indexPathOfGivenCell] withRowAnimation:UITableViewRowAnimationNone];
You should be able to place the following in the method.
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
You can ask to redraw cell at visible rows:
NSArray *visibleIndexPaths = [tableView indexPathsForVisibleRows];
[self.tableView reloadRowsAtIndexPaths:visibleIndexPaths withRowAnimation: UITableViewRowAnimationFade];

Scrolling a UITableView cell up and then expand it

I have a UITableView with custom cells in it. The cells expands when I tap on them. I am facing an issue when I tap on the cell which is half cut by the bottom screen edge of the device. When I tap on this cell, I want cell to move up and then expand to show the details but right now it just expands and I do not know if is expanded or not. I need to manually scroll the table view up to see it.
Below is my cell tapping code. I implemented the related cell expansion methods and cell is expanding properly.
- (void)cartTableView:(UITableView *)iTableView didSelectRowAtIndexPath:(NSIndexPath *)iIndexPath {
[self.temporaryCartTable scrollToRowAtIndexPath:iIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[self expandOrCollapseCartCellAtIndex:iIndexPath];
}
- (void)expandOrCollapseCartCellAtIndex:(NSIndexPath *)iIndexPath {
self.selectedIndexPath = iIndexPath;
[self.temporaryCartTable beginUpdates];
[self.temporaryCartTable reloadRowsAtIndexPaths:#[iIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.temporaryCartTable endUpdates];
}
After expanding, call scrollToRowAtIndexPath, which should position your cell correctly.
You need to check if rect of the cell you're extending (rectForRowAtIndexPath:) is fully within the bounds of the table view (based on contentSize and contentOffset, using CGRectContainsRect and if it's not - and only partially visible - then you need to scroll it to the bottom/top of the table view.

UITableView animation glitch when deleting and inserting cells with varying heights

I am having a problem with the animation that UITableView provides when deleting and inserting a cell at the same time.
I have a list of cells lets call them questions. When one question is tapped it should add a cell beneath itself to display the answer to that question. If another answer is already being displayed that answer should be removed from the table.
The issue arises when the cell being inserted is very tall. If it is so tall that it's eventual bounds encroach into the space that the cell to be deleted takes up then during the animation we see the through the answer cell that we are deleting to see the cell that is being added
(see link to video of problem)
the is what my code looks like to move around the cells in the table view
[tableView beginUpdates];
if (deleteIndex) {
[tableView deleteRowsAtIndexPaths:#[deleteIndex] withRowAnimation:UITableViewRowAnimationTop];
}
if (addIndex) {
[tableView insertRowsAtIndexPaths:#[addIndex] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
I have tried
[tableView beginUpdates];
if (deleteIndex) {
[tableView deleteRowsAtIndexPaths:#[deleteIndex] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
//do stuff to update data source
[tableView beginUpdates];
if (addIndex) {
[tableView insertRowsAtIndexPaths:#[addIndex] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
But because there's no callback confirming that the table view did complete the first set update before starting the second block pretty much the same problem occurs. I know I could use perform selector with delay, but this seems like a bandaid.
Second I tried to encompass the animation in a block like this
[UIView animateWithDuration:0.0 animations:^{
[tableView beginUpdates];
if (deleteIndex) {
[tableView deleteRowsAtIndexPaths:#[deleteIndex] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
//do stuff to update data source
} completion:^(BOOL finished) {
[tableView beginUpdates];
if (addIndex) {
[tableView insertRowsAtIndexPaths:#[addIndex] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
}];
Again, because the completion block is fired after we call endUpdates not after the updates actually complete this does not resolve the use.
I also went into storyboard to be sure that clip subviews to bounds is selected for the cells. Again, this does not resolve the issue because we are not seeing a subview of the cell expand beyond it's expected height.
Looking closer at the animation by slowing it down it looks like apple inserts the cell to be added under the cells that won't be changed in the table and then moves the cells that will remain into their new positions. As a result the cell that was deleted becomes a transparent window where we see what they are actually doing under the hood.
The right approach would be to first remove the old answer and then after the animation is over add the new answer. There is a UITableViewDelegate method that is triggered after the cell animation is complete.
tableView:didEndDisplayingCell:forRowAtIndexPath:
Inserting the new answer row within this delegate method will result in the correct animation.
There are a few details to keep in mind- You'll need some logic to ensure that the correct cell height is returned and that the correct number of expected rows in the section is returned. Those data source methods are called after we remove our old answer in and again when we add the new one when we call
endUpdates
on the table view
Using anything other than UITableViewRowAnimationTop results in some strange animation behavior. This is because the content view of the cell is not what is being animated.
i solve same problem in my project by hide control like:
in ~cell.m have
(void)setSelected:(BOOL)selected animated:(BOOL)animated;
{
if(selected == NO)
{
self.selectedView.hidden = YES;
self.descriptionLabel.hidden = YES;
}
else
{
self.selectedView.hidden = NO;
self.descriptionLabel.hidden = NO;
}
}
may it still helpful

Resources