ios 如何获取当前正在显示的tableView的indexPath.row?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5509571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get indexPath.row of tableView which is currently being displayed?
提问by Parth Bhatt
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:
在实施克里希那巴德拉的回答时出现以下错误:
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?
可能有什么问题?
回答by Krishnabhadra
You can use tableView indexPathsForVisibleRows
function, which returns an NSArray
of NSIndexPath
for all visible UITableViewCell
. From indexPath.row
you can find your array index.
您可以使用的tableViewindexPathsForVisibleRows
函数,它返回NSArray
的NSIndexPath
所有可见UITableViewCell
。从indexPath.row
你可以找到你的数组索引。
NSArray *visible = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];
indexPath.row
will give you index of first object displayed.
indexPath.row
会给你显示的第一个对象的索引。
回答by Adarsh V C
[self.myTableView visibleCells];
NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.myTableView indexPathsForVisibleRows]];
NSIndexPath *indexPath= [anArrayOfIndexPath lastObject];
Hope this will help you.
希望这会帮助你。
回答by Anand
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. Returns nil if no rows are visible.
一个 NSIndexPath 对象数组,每个对象代表一个行索引和部分索引,它们一起标识表视图中的可见行。如果没有行可见,则返回 nil。
回答by Nazmul Hasan
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)
}
回答by j_freyre
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.
这将返回一个 UITableViewCell 对象的 nsarray,每个对象代表接收表视图中的一个可见单元格。