How to call ScrollViewDidScroll delegate method of UITableView in IOS - ios

I have a UITableView in that I am loading photos with comments..
I have horizontal scrolling of images in each row and vertical scrolling of users photos horizontally with comments..
I want to update the single cell when ever the user commented the photo.
At scrollviewDidScroll method I have code for dynamically updated the height of UITableviewCell and displaying the comments. As my requirement is dynamically increase height of custom cell with respect to comments of the photo scrolled. I am displaying latest three comments in the cell. It should dynamically updated the height based on 0 or 1 or 2 or 3 comments of that photo..
So, I have tried with below code..For updating the single row..
[self.photosTable beginUpdates];
[self.photosTable reloadRowsAtIndexPaths:[self.photosTable indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.photosTable endUpdates];
[self scrollViewDidScroll:self.photosTable];//As UITableView is sub class of UIScrollview I wrote like that
But scrollviewDidScroll is not calling. If I scroll the table then only it is calling..
Please suggest the ideas where I went wrong..
Thanks in Advance..

scrollviewDidScroll is a delegate method which is automatically called when the UITableView is scrolled.
If you want to scroll the UITableView, try and use this code :
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];

As others have pointed out, you should not call your own delegate methods.
This seems like an XY problem.
Take a step back and explain what you are trying to accomplish here. Why do you need your scrollviewDidScroll method to be called?

Related

Getting first Cell/Item of UICollectionView while scrolling - iOS

I have a UICollectionView with 1 row 1 column. I want to get the first item (indexPath basically) when I am scrolling it horizontally.
For example, I have got 100 items displayed horizontally in my UICollectionView and when I scroll from right to left, whichever item is the first one visible, I need its indexPath.
Which means the indexPath will be constantly changing while its scrolling.
How to achieve this? Please Guide. Thanks!
collectionView.indexPathsForVisibleItems() returns an array of NSIndexPath for visible cells. You can get the left most one by sorting them by NSIndexPath.item.
Edti:
To be notified while scrolling, implement the UIScrollViewDelegate method scrollViewDidScroll(_: UIScrollView). Remember that UICollectionView is a subclass of UIScrollView.
I think you need implement UIScrollView delegate method and get visible cell inside
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UICollectionViewCell *firstCell = [[collectionView visibleCells] firstObject];
NSIndexPath *firstIndexPath = [collectionView indexPathForCell: firstCell];
NSLog(#"%#", firstIndexPath);
}
Hope this help

Swift 2.0 / UITableViewCell - cant get action to happen in cell in top position when scrolling

My goal is to be able to auto scroll through my UITableView cells while automatically performing an action on the cell that is in the top visible position in the view. I've already figured out how to auto scroll through my UITableView using the various scrollview delegate methods, but im still having trouble figuring out how to call an action on the desired cell once it's in the correct position in the feed.
Does anyone have any suggestions?
You can use this :
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:desiredrow inSection:desiredsection];
[yourTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];

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

How do you force a specific row to be visible in UITableView when initially loading the table without any animation?

It has been suggested that one scroll to the desired row in viewWillAppear, but this does not work with iOS 7. I have only been able to make this work in iOS 7 in the viewDidAppear callback. Unfortunately, you see the desired row scroll into view. I don't want to see any scrolling, I simply want the row to be visible when loaded. Can anyone suggest the proper way to do this in iOS 7?
It probably did not work in viewWillAppear, because that table had no data at this point.
Add [tableView reloadData];and it should work.
Let me get this straight: you want your table view to show a certain row at the top when the view apperas? Yes?
If so, you want:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
with your cell indexPath, UITableViewScrollPositionTop as scrollPosition and animated NO like so
[tableView scrollToRowAtIndexPath:myExampleindexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
If you know the cell index then it's as simple as:
[tableView setContentOffset:CGPointMake(cellLocation.x,cellLocation.y) animated:NO];
Call that just after you load your tableView data and it will scroll to your cell being on top. There are other options as well:
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:currentSection] animated:NO scrollPosition:UITableViewScrollPositionTop];
Use this code with whatever scrollPosition you would like and Apple takes care of the bounding to the table (whereas setting the scrolling position is all user defined, it could be out of the table's view).
EDIT:
You could surround your selecting code with a call to UIView setting no animations allowed. That has worked for me in the past with different things, but I have never tried it in viewDidLoad.
[UIView setAnimationsEnabled:NO];
//Scroll the tableview
[UIView setAnimationsEnabled:YES];

How to scroll two view simultaneously in ios?

I have bar chart view where i can scroll the bars.And I have tableview such that when im scrolling the barchart simultaneously tableview should scroll.Just give idea to do with touch or gesture methods.
Refer scrollToRowAtIndexPath:atScrollPosition:animated method in UITableView class reference.
I guess this method will help you for your requirement which you mentioned.
when you are scrolling the bar, take the event and call this method:
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:indexPath.section]
atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

Resources