ios – 如何获取当前正在显示的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返回NSIndexPath的NSArray.从indexPath.row您可以找到您的数组索引.
NSArray *visible       = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];

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

以上是来客网为你收集整理的ios – 如何获取当前正在显示的tableView的indexPath.row?全部内容,希望文章能够帮你解决ios – 如何获取当前正在显示的tableView的indexPath.row?所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。