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将为您显示第一个对象的索引.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...