How to scroll two view simultaneously in ios? - 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];

Related

Scrolling UITableView to specific position using setContentOffset not working properly - ObjC

I saw many answers in SO which describes how can I scroll UITableView to the specific position.
I use setContentOffset for scrolling to particular position.
I meet a weird issue. If my tableView is half scrolled then setContentOffset is working properly. But when I scroll to end of the tableView and then use setContentOffset, table view scrolls a little bit more.
My setContentOffset value is CGPoint(0,199).
[self.tableView setContentOffset:CGPointMake(0,199) animated:NO];
After reaching a bottom I use setContentOffset. Then I check the contentOffset of UITableView. and it is 169.
I am not able to figure it out what exactly the issue is.
Edit::
I am using the following code. When a button in the last cell is pressed:
- (void)userPressedSubmitButtonOnLastCell
{
[self updateData];
[self.tableView reloadData];
[self performSelector:#selector(scrollTableView) withObject:nil afterDelay:0.5];
}
- (void)scrollTableView
{
[self.tableView setContentOffset:CGPointMake(0,199) animated:NO];
}
Why don't you use tableView scrollToRowAtIndexPath: or tableView scrollRectToVisible:(CGRect)? This will allow you to scroll to either specific row or rectangle. ContentOffset is more suitable for UIScrollView.

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

How to call ScrollViewDidScroll delegate method of UITableView in 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?

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

Resources