Xcode iOS 7 Issue with Delete Row Animation - ios

I have a Table view. When the user swipes to the left the delete button appears and he is able to remove this entry. But now I've made some strange experiences with the row animation. When the user then clicks on the delete button it stays there but the normal cell is moved. After the cell content view has disappeard the delete button exits also.
What's the problem here?
[[eventDataDictionary objectForKey:key] removeObject:[sortedArray objectAtIndex:indexPath.row]];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[tableView endUpdates];
Thanks and best regards from Germany,
Chris

I don't think you need beginUpdates, endUpdates. deleteRowsAtIndexPaths: has its own animation parameter. Just remove the begin~end end try again.

Related

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];

UITableView delete section causes strange animation

I have a UITableView that is made up of sections with 1 row in each section. This is done so that I can get buffer space between each row. When the user deletes a row, they are de facto deleting the whole section.
My row deletion code in commitEditingStyle is:
[self.habitsToView removeObjectAtIndex:[indexPath section]];
[tableView beginUpdates];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
But when a row is deleted, the animation is wrong. The row itself slides off to the left, and the delete button slides upwards. This happens no matter what value I use for withRowAnimation
Anyone know what could be causing the strange animation?

Want a Cell that Drops Down Below Activated Cell

While writing a UITableView in iOS8 I've implemented swipe left/right gestures similar to that in Mail. I would now like to implement a cell that slides down below the cell which the user has just swiped left and tapped a button on. Can anyone point me in the right direction?
You need to add the row that should slide out to the data model (UITableViewDataSource) of your UITableView, then tell the tableview section to update with an animation:
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];

iOS Table view cell not displaying properly after resize

I have set up my table view with 6 static cells and I'm trying to have one cell (the third cell) expand upon selection. The problem is, it resizes after calling reloadRowsAtIndexPaths:, but then the cell appears blank. If I scroll the cell out of view, it shows the contents properly.
When I call [tableView reloadData] the cell isn't blank, but then, I can't enjoy the animation.
Does anyone know why this is so?
Well if you set the height for a row then you need to reload the content of the cell NOT the content of the whole table. If you call [tableview reloadData] then it simply means that you redraw the whole table(without the height correction).
To reload a single cell in the table:
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
I don't know if I was the only one having the problem with a blank static cell issue.
I stumbled on a solution and that is calling [tableView reloadData] after calling reloadRowsAtIndexPaths:
Reload the respective table view row using the main thread.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Reload the respective table view row using the main thread.
[self.tblFiles reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:index inSection:0]]
withRowAnimation:UITableViewRowAnimationNone];
}];

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