An Strange UITableView scrollToRowAtIndexPath behaviour - uitableview

When I Use
[self.chatContentTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:bottomFlag+2 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
when the animated set YES,my tableView will scroll,but when the animated set NO,it donest work

I also found it.
My code is:
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]atScrollPosition:UITableViewScrollPositionMiddle animated:animation];
It work after remove reloadData.
I guess the reason why it does not take effect is that it's executed immediately after reload

Related

iOS 8.4 UITabelview scrollToRowAtIndexPath is not working in Device

I have UITableview which has dynamic row height using AutoLayout.
In iOS 8.4, if set scrollToRowAtIndexPath or setContentOffset its not scroll to last indexpath. in iOS 9.x it is working. Kindly help
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:arr.count - 1 inSection:RootArr.count - 1] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
dispatch_after(0, dispatch_get_main_queue(), ^{
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
});
It is OK

UITableView - using reloadRowsAtIndexPath

I have aUITableView with delegate methods declared. ThenumberOfRows &numberOfSections are updated dynamically.
(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{}
(NSInteger)numberOfSectionsInTableView:
Instead of using the [tableView reloadData], I would need to usereloadRowsAtIndexPath.
However I get a crash with invalid number of rows. How do I handle the dataSource changes? Lets says I have an array of count 10, and when I reload theUITableView usingUIRefreshControls by pulling, and the data from the server. From 10, if the count of array drops to 1, how will I handlereloadRowsAtIndexPath?
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathForVisibleRows] withRowAnimation:UITableViewRowAnimationNone]
There seems to be a crash when this is called. How would I handle change in data array's count?
To reload specific cells of Tableview the method is reloadRowsAtIndexPaths: not reloadRowsAtIndexPath
[self.tableview reloadRowsAtIndexPaths:[self.tableview indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
However for reloadRowsAtIndexPaths to work properly datasource needs to be consistant. As Indexpaths are already loaded.
If you still use this method, You will get either arrayIndexOutofBound error or duplicate values in cells(depending on increase or decrease in datasource).
//call begin and end updates while performing some hard core actions on tableview
[My_Table beginupdates];
[My_Table reloadRowsAtIndexPaths:[My_Table indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
[My_Table endupdates];
hope it will helps you.
you can try with this.
[tblMe beginUpdates];
[tblMe reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
[tblMe endUpdates];
Hope this may help !!!

UITableViewRowAnimationNone - behaving as UITableViewRowAnimation(Top/Bottom)

I have set the row insertion with the following code. I am only using UITableViewRowAnimationNone when inserting and deleting the rows, but sometimes ,as you can see in the gif below, the row animates in from the top or bottom. For the most part it doesn't animate, as I want it, but sometimes it animates on insertion and deletion. I am not talking about the table view expanding to show the inserted cell, I mean the cell appears to be sliding in from the bottom or top.
Here is the method that controls the insertion animation:
- (void)contentHeaderFooterView:(NFContentHeaderFooterView *)headerFooterView sectionOpened:(NSInteger)section{
NSIndexPath *pathToAdd = [NSIndexPath indexPathForRow:0 inSection:section];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[pathToAdd] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
And here is the method that controls the deletion animation.
- (void)contentHeaderFooterView:(NFContentHeaderFooterView *)headerFooterView sectionClosed:(NSInteger)section{
NSIndexPath *pathToDelete = [NSIndexPath indexPathForRow:0 inSection:section];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[pathToDelete] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
I've had similar issues with table updates not performing the animation I gave it directly. I can't say for sure why, but one thing I can suggest is, instead of doing a specific row deletion between begin/endUpdates, you can just to a hard reloadData.
Answered here: https://stackoverflow.com/a/37352789/577237
Reposted since it's brief:
[UIView performWithoutAnimation:^{
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:rowsToRemove withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}];
Note that this is iOS 7.0+

UITableView selecting row programmatically not having expected effect

I have a tableview with (say) five rows in the only section. The code to edit a custom cell all works, but the problem occurs when I need to reload the table data with new data that has fewer rows, say two. The programme crashes if I have been editing a row that has a higher index than the maximum number of rows in the reloaded table. The exception shows that it is trying to access the higher numbered element beyond the limit of the new table number of rows.
I have tried putting:
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
in just before the
[self.tableView reloadData];
but it doesn't work.
It can only mean that you are accessing the first row here:
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
when you do not have the first row.
When you are reloading the tableview, make sure you have atleast one row.
Try doing this:
if([self.tableview numberOfRowsInSection:0]>0)
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
I believe you used the code below to unselect your cell
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
If so you should use this instead
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
In the end, deselecting the old row (i.e. whilst the old data source was still current) and then selecting the first row with the new data source fixed the problem.

ios reload tableview in beginUpdates

I change the height of a cell (when image downloaded) with method like that
-(void)didHeightChanged:(float)imageHeight atIndex:(NSInteger)indexPath
{
[dictionary setObject:imageHeight forKey:indexPath];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
Everything works well with first data, but when I reload UITableView with second data , probably beginUpdates method doesnt ended and I got issues - when I delete and reload UITableView, cell dont disappear, UITableView shows wrong cells
How can I stop beginUpdates immediately before reload new data?
You should either call [self.tableView reloadData] or [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathWithRow:index inSection:0]]]; // or indexPath if instead of passing a NSInteger you pass a NSIndexPath * to the method.
I found the answer
[self.tableView beginUpdates];
[self.tableView endUpdates];
it was called in background thread...

Resources