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

Par*_*att 9 cocoa-touch uitableview nsarray ios4 ios

我有一个包含许多值的tableView.

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

我怎样才能做到这一点?

我在实现krishnabhadra的答案时得到以下错误:

出现错误的行是:

[self.table scrollToRowAtIndexPath:indexVis atScrollPosition:UITableViewScrollPositionTop animated:YES];
Run Code Online (Sandbox Code Playgroud)

错误:

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.'
Run Code Online (Sandbox Code Playgroud)

可能有什么不对?

Kri*_*dra 23

您可以使用的tableView indexPathsForVisibleRows函数,它返回NSArrayNSIndexPath所有可见UITableViewCell.从indexPath.row你可以找到你的数组索引.

NSArray *visible       = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

indexPath.row 将为您显示第一个对象的索引.