I've been banging my head on this problem for hours, but every time I try something like this:
self.dataArray.append(newCellObj)
and then I do this:
self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
self.tableView.endUpdates()
The UITableView will automatically scroll to the top of the page.
Even if I try:
self.tableView.scrollEnabled = false
self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
self.tableView.endUpdates()
The UITableView will still scroll to the top even with scrolling completely disabled. What exactly causes the ScrollView to scroll to the top after insertRowsAtIndexPaths is called?
The only solution I have for this issue is to use this:
self.tableView.reloadData()
instead. If I use reloadData instead than it's fine, but then I lose the nice animation which I'd really like to keep.
I also have self.tableView.scrollsToTop = false and I've tried many other configurations like that that could disable scrolling somehow, but, there's something that overrides this after insertRowsAtIndexPaths
I was encountering the same issue as OP. Additionaly, sometimes some of my table view cells would go "blank" and disappear altogether, which led me to this related question.
For me, the solution was to do ONE of the following:
implement func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
disable auto layout
set a more accurate estimatedRowHeight on my UITableView
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
// I'm use auto layout and this variant without animation works
// ...insert object to datasource
NSUInteger idx = [datasource indexOfObject:myNewObject];
if ( NSNotFound != idx )
{
NSIndexPath *path = [NSIndexPath indexPathForRow:idx inSection:0];
[self.table beginUpdates];
[self.table insertRowsAtIndexPaths:#[path]
withRowAnimation:UITableViewRowAnimationNone];
[self.table endUpdates];
[self.table scrollToRowAtIndexPath:path
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
}
Related
In my app I am trying to display Mkmapview in UITableView. When I tap the cell, it has to expand and show the map. Then if I tap another cell, it should act again expand and show gallery view , the previous cell must collapse and map should be hide.
thanks in advance,
You have to keep track of the state (or height) of the expanding/collapsing cell. Then report that height in tableView:heightForRowAtIndexPath:. Then add the code below to trigger the resizing.
[self.tableView beginUpdates];
[self.tableView endUpdates];
For example:
- (void)toggleMapCell {
_mapCellExpanded = !_mapCellExpanded;
if (_mapCellExpanded)
_mapCellHeight = 200.0;
else
_mapCellHeight = 44.0;
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqualTo:_mapCellIndexPath])
return _mapCellHeight;
else
return 44.0;
}
If you have to re-layout the cell's content. I think the best place is to do it in the cell's layoutSubviews.
I have UITableView with cells. When i select cell - height increased, when i select this cell again - height decreased. In method "didSelectRowAtIndexPath" i use this -
NSArray *indArr = [[NSArray alloc] initWithObjects:(NSIndexPath*)[timer userInfo], nil];
[self.tableView reloadRowsAtIndexPaths:indArr withRowAnimation:UITableViewRowAnimationFade];
But "UITableViewRowAnimationFade" animation has faded effect, and cell is flickered.
I tried another animations, but they also unsuitable(
How i can avoid this effect and update cell without animation?
Setting the row animation to .None doesn't work - there is still stuff going on and moving about. In my case, some cells changed height, which caused the table view to scroll.
To hold perfectly still, do this:
UIView.performWithoutAnimation({
let loc = tableView.contentOffset
tableView.reloadRows(at: [indexPath], with: .none)
tableView.contentOffset = loc
})
Try this.
UIView.setAnimationsEnabled(false)
self.yourTableView.beginUpdates()
self.yourTableView.reloadRows(at: yourIndexPath, with:UITableViewRowAnimation.none)
self.yourTableView.endUpdates()
Have a look in the documentation of UITableView, there is a value for the rowAnimation called UITableViewRowAnimationNone, which won't animate anything. Try this instead:
[self.tableView reloadRowsAtIndexPaths:indArr withRowAnimation:UITableViewRowAnimationNone];
When a user taps a button in one of my rows I am updating the underlying model for that row and then calling reloadRowsAtIndexPaths for the given row (i.e. single row reload).
- (IBAction)handleCompleteTouchEvent:(UIButton *)sender {
NSIndexPath *indexPath = [self.tableView indexPathForView:sender];
id item = [self dataForIndexPath:indexPath];
if ([item respondsToSelector:#selector(completed)]) {
// toogle completed value
BOOL completed = ![[item valueForKey:#"completed"] boolValue];
[item setValue:[NSNumber numberWithBool:completed] forKey:#"completed"];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
}
The problem is that the table view bounces back to the top of the section after making this call. How can I prevent this from occurring and keep the scroll position where it is?
Ah Ha! I found the problem and am going to answer my own question for the poor soul who runs into this issue in the future.
All of my cells have variable height so I was using the new iOS7 method in UITableViewDelegate thinking it might speed up render time (not that I really needed it):
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
Anyway, implementing this method has the evil side effect of causing the table to bounce to the top of the section when calling:
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
To solve the bounce problem I just removed the override of that estimatedHeightForRowAtIndexPath method and now everything works as it should. Happy at last.
This did the trick for me.
UIView.setAnimationsEnabled(false)
tableView.reloadRows(at: [...], with: .none)
UIView.setAnimationsEnabled(true)
Swift 4.2
This can work anywhere you want to remove animation.
During reload of table , table section or any row
UIView.performWithoutAnimation({
cell.configureSelection(isSelected: true)
tableView.reloadSections([1], with: .none)
tableView.allowsSelection = false
})
You should be able to do what you are trying to do by changing the cell contents directly. For example, if you are using the base UITableViewCell class and the data in your model is a NSString which you show in the table view cell, you can do the following (after you change your data model) instead of calling reloadRowsAtIndexPaths:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *label = [cell textLabel];
label.text = #"New Value";
If you are using a custom subclass of UITableViewCell, it's roughly the same except for accessing the views of the cell will need to be done through the contentView property.
It may be possible to put the cell in it's own section and call reload section:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
this appears to fix the issue partially. Not the cleanest way but it may work for you.
In my similar case, I had to tweak the implementation of method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
where my heights were being reset. So, instead of resetting height for every cell I updated only the effected cells for reloadRowsAtIndexPaths call.
If you know the minimum height of your cell, you must specify that while setting estimatedRowHeight. I was setting it to 1 as I have read before somewhere that any value above 0 would suffice the purpose, but it was the culprit.
When I set it to 44, which was the minimum height my cell could have, all went fine.
Dynamic heights were also working fine, no issues with this fix.
To build off xsee's answer -
I had set the Estimate in interface builder to "automatic". I changed this to another number and it started working. I kept Row Height to automatic.
I had the same issue. I ended up just calling tableView.reloadData() instead after updating my data / cell and it didn't bounce back to the top / data was updated in place - FYI
This solved this issue for me with Swift 5 / iOS 14:
UIView.performWithoutAnimation({
self.tableView.reloadRows(at: [self.idxTouched], with: .none)
})
I'm using [beginUpdated] to expand and collapse cells in my tableview. Works as expected however there is a side effect. The top border of the selected cell disappears. To repro make a tableview with selection style none. Use the code below you will see that selecting a row makes the border disappear. Any suggestions? I've tried unsetting separator style and resetting to singleLine. I've tried layoutIfNeeded.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[tableView endUpdates];
}
This seems to be core SDK error because I reproduced in a new project with nothing but the code above and cell selection style of none.
Any work arounds? Using a 1 px UIView as a pseudo border is not an option because comps call for a group style look and figuring out if a reuseable cell should have inset or full border for every cell is just not practical.
I solved this by deselecting the cell after the updates.
Objective-C:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[tableView beginUpdates];
[tableView endUpdates];
[tableView delesectRowAtIndexPath:indexPath]
}
Swift:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableview.beginUpdates()
tableview.endUpdates()
tableview.deselectRowAtIndexPath(indexPath, animated: true)
}
I have a custom tableFooterView (it's just an 8px high CoreGraphics arc with a gradient) that I set with the tableFooterView property in viewDidLoad rather than viewForFooterInSection. When setting it with viewForFooterInSection, it floated over the content when it reached the bottom, whereas tableFooterView does what I want it to in that it stays with the UITableView's height.
But when the cells or table view do change in height, the tableFooterView animates to them slowly (about half a second but it's very noticeable). This is pretty awkward since the footer is supposed to look like an extension of the last cell. For instance, when heightForRowAtIndexPath changes the height of a cell, the tableFooterView kind of ghost-floats back. In this screenshot the bottom cell has just been shrunken to its normal size and the footer is floating back.
(As a new user I can't post images but here's the link: http://desmond.imageshack.us/Himg835/scaled.php?server=835&filename=iossimulatorscreenshotj.png&res=landing)
(This content is no longer available, 14/9/15).
It will also float over the content when the height of the last cell is suddenly changed to be larger than it was.
Any pointers? Thanks very much.
Edit: By cells changing in height, I mean through the heightForRowAtIndexPath section:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Note *currentNote = [self.notes objectAtIndex:indexPath.row];
if (currentNote.associatedCellIsSelected) {
return NORMAL_CELL_FINISHING_HEIGHT*2;
}
return NORMAL_CELL_FINISHING_HEIGHT;
}
Edit 2: In didSelectRowAtIndexPath I make the cell selected (actually the cell's note), begin / end updates as well as call reloadRow for the row that's been selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Note *currentNote = [self.notes objectAtIndex:indexPath.row];
if (currentNote.associatedCellIsSelected == FALSE) {
currentNote.associatedCellIsSelected = TRUE;
[tableView beginUpdates];
[tableView endUpdates];
}
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }
I've also made sure that I get the same behavior with a plain rect redColor UIView in place of the Core Graphics footer, with the same results. I wish there was just an easy way to override the footer and tell it to not animate!