How to detect which UITableViewCell is at the top of a UITableView - ios

I have a view with a map and and list displayed at the same time. I would like the map to move when the user scrolls the list.
I was hoping for something like
(UITableView*)tableView didScrollTableViewCellToTop:(UITableViewCell *)cell
I haven't found something that will support this yet.
Thanks in advance

You can get all the visibleCells by running...
NSArray *cells = [self.tableView visibleCells];
This returns an array of UITableViewCells.
You can then find the one with the lowest indexPath.row value to find the top one.

Remember that the UITableView is simply a scroll-view. So you can get the offset of the scroll view and use the position to get the cell at that location:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: point];
To continuously monitor and change, implement a scroll-view-delegate and implement the scrollViewDidScroll method.

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

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?

Get NSString from HeaderView Section on top

I want to get a NSString out of the table HeaderView that's on top that moment. Because I want to implent it in ScrollViewDidScroll so one label changes when the header view changes.
Didn't find any clues on the web how to do this
Thanks!
There is no predefined method by which you can get top section. There is one trick by which you can achieve it. First get indexPath for all visible cells.
NSArray *visible = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];
Now you can get section value by indexpath.section. After getting this section you can get string from your data array for this section.

iOS Table View Finding A Cell

I have a table view with custom cells. Cells have an image view, a label and a progress view. What I want to achieve is to update progress view in a specific cell according to the event that fires. My idea is to find the cell by name label that is in the event and then update progress view. This may not be an efficient way but I couldn't think of some other way. Other ideas are also appreciated. Thanks in advance.
You could use a NSDictionary with the name as the key and the NSIndexPath as the object. Then to can look up the index path for a given cell using the name in the dictionary.
UITableView has a 'cellForRowAtIndexPath:` method that should give you the cell for a specific row.
i.e. something like
// Get visible cell for data item
NSInteger row = [myArrayOfThings indexOfObject:thing];
NSIndexPath *inedexPath = [NSIndexPath alloc] initWithRow:row section:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indeexPath];
// Update cell here ...
...
Disclaimer : Code typed from memory - you may have to look at the documentation for some of the method names :)
the more better way is with tag,
you can give each cell a unique tag and access that cell by this method
//method of UIView(UIViewHierarchy)
- (UIView *)viewWithTag:(NSInteger)tag;
// can be used like this
UITableViewCell *cell = (UITableViewCell*)[tableView viewwithtag:tag];

Resources