简体   繁体   English

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

[英]How to get indexPath.row of tableView which is currently being displayed?

I have a tableView with many values. 我有一个包含许多值的tableView。

I want to get the first indexPath.row value of the table which is being displayed currently. 我想获取当前显示的表的第一个indexPath.row值。

How can I do that? 我怎样才能做到这一点?

I get Following Error while implementing krishnabhadra's answer: 我在实现krishnabhadra的答案时得到以下错误:

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? 可能有什么不对?

You can use tableView indexPathsForVisibleRows function, which returns an NSArray of NSIndexPath for all visible UITableViewCell . 您可以使用tableView indexPathsForVisibleRows函数,该函数为所有可见的UITableViewCell返回NSIndexPathNSArray From indexPath.row you can find your array index. indexPath.row您可以找到您的数组索引。

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

indexPath.row will give you index of first object displayed. indexPath.row将为您显示第一个对象的索引。

[self.myTableView visibleCells];

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

NSIndexPath *indexPath= [anArrayOfIndexPath lastObject];

Hope this will help you. 希望这会帮助你。

you can use indexPathsForVisibleRows 你可以使用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. 一组NSIndexPath对象,每个对象表示行索引和段索引,它们共同标识表视图中的可见行。 Returns nil if no rows are visible. 如果没有可见的行,则返回nil。

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

That will return a nsarray of UITableViewCell objects, each representing a visible cell in the receiving table view. 这将返回一个UARableViewCell对象的数组,每个对象代表接收表视图中的一个可见单元格。

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

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)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM