11

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?

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216

5 Answers5

26

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.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • Thanks I will try that out soon and get back to you :) – Parth Bhatt Apr 01 '11 at 05:51
  • Where should I write this code? On `cellForRowAtIndexPath` method? – Parth Bhatt Apr 01 '11 at 05:52
  • you can call it from anywhere..Call it where you want the list of visible cells.. – Krishnabhadra Apr 01 '11 at 05:56
  • I scroll to some position and then switch to another tab in my tabbar and then comeback onto same tableView again, then I want that the tableView should display the same cells that It displayed before the switching. So in that case where should I write the code? – Parth Bhatt Apr 01 '11 at 06:13
  • 1
    in the viewWillDisappear of the code write the above code to get the indexpath of visible array...Then in the viewWillAppear use scrollToRowAtIndexPath function of your tableView to scroll to the indexpath you saved in the array before..Hope this helps – Krishnabhadra Apr 01 '11 at 06:26
3
[self.myTableView visibleCells];

NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.myTableView indexPathsForVisibleRows]];

NSIndexPath *indexPath= [anArrayOfIndexPath lastObject];

Hope this will help you.

Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
3

you can use indexPathsForVisibleRows

Returns an array of index paths each identifying a visible row in the receiver.

- (NSArray *)indexPathsForVisibleRows

Return Value

An array of NSIndexPath objects each representing a row index and section index that together identify a visible row in the table view. Returns nil if no rows are visible.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Anand
  • 2,086
  • 2
  • 26
  • 44
0

You can use: [self.tableView visibleCells]

That will return a nsarray of UITableViewCell objects, each representing a visible cell in the receiving table view.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html

j_freyre
  • 4,623
  • 2
  • 30
  • 47
0

Get exact index number at center currently being displayed:

 let middleIndex = ((tableView.indexPathsForVisibleRows?.first?.row)! + (tableView.indexPathsForVisibleRows?.last?.row)!)/2

Example:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

  let middleIndex = ((tableView.indexPathsForVisibleRows?.first?.row)! + (tableView.indexPathsForVisibleRows?.last?.row)!)/2
  print(middleIndex)

}
Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73