如何获得当前正在显示的tableView的indexPath.row?

我有一个很多值的tableView。

我想获得当前正在显示的表的第一个indexPath.row值。

我怎样才能做到这一点?

我在执行krishnabhadra的回答时遇到以下错误:

发生错误的行是:

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

错误:

 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.' 

什么可能是错的?

你可以使用tableView indexPathsForVisibleRows函数,它为所有可见的UITableViewCell返回一个NSIndexPathNSArray 。 从indexPath.row你可以find你的数组索引。

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

indexPath.row会给你显示第一个对象的索引。

你可以使用indexPathsForVisibleRows

返回一组索引path,每个索引path标识接收器中的可见行。

 - (NSArray *)indexPathsForVisibleRows 

返回值

一个NSIndexPath对象数组,每个对象表示一个行索引和一个部分索引,它们共同标识表视图中的可见行。 如果没有行可见,则返回nil。

 [self.myTableView visibleCells]; NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.myTableView indexPathsForVisibleRows]]; NSIndexPath *indexPath= [anArrayOfIndexPath lastObject]; 

希望这会帮助你。

你可以使用: [self.tableView visibleCells]

这将返回一组UITableViewCell对象,每个对象表示接收表视图中的可见单元格。

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