Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get indexPath.row of tableView which is currently being displayed?

I have a tableView with many values.

I want to get the first indexPath.row value of the table which is being displayed currently.

How can I do that?

I get Following Error while implementing krishnabhadra's answer:

Line at which the error comes is:

[self.table scrollToRowAtIndexPath:indexVis atScrollPosition:UITableViewScrollPositionTop animated:YES];

ERROR:

Assertion failure in -[NSIndexPath row], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableViewSupport.m:2018
2011-04-01 11:57:25.458 GlossaryPro[1525:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView.  Index paths passed to table view must contain exactly two indices specifying the section and row.  Please use the category on NSIndexPath in UITableView.h if possible.'

What could be wrong?

like image 800
Parth Bhatt Avatar asked Apr 01 '11 05:04

Parth Bhatt


1 Answers

You can use tableView indexPathsForVisibleRows function, which returns an NSArray of NSIndexPath for all visible UITableViewCell. From indexPath.row you can find your array index.

NSArray *visible       = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];

indexPath.row will give you index of first object displayed.

like image 188
Krishnabhadra Avatar answered Sep 22 '22 16:09

Krishnabhadra