ios 如何知道 UITableView.visibleCells 的索引路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8321973/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:40:38  来源:igfitidea点击:

how to know indexpath of UITableView.visibleCells?

iosobjective-cuitableview

提问by user4951

NSArray * arrayOfVisibleTVC=[BNUtilitiesQuick ListController].tableViewA.visibleCells;
NSLog(@"array : %@", arrayOfVisibleTVC);

will display

会显示

"<UITableViewCell: 0x76c22a0; frame = (0 0; 320 75); autoresize = W; layer = <CALayer: 0x76bf770>>",
"<UITableViewCell: 0x76cfec0; frame = (0 75; 320 75); autoresize = W; layer = <CALayer: 0x769d260>>",
"<UITableViewCell: 0xe245f70; frame = (0 150; 320 75); autoresize = W; layer = <CALayer: 0xe245ed0>>",
"<UITableViewCell: 0xe248980; frame = (0 225; 320 75); autoresize = W; layer = <CALayer: 0xe2488c0>>",
"<UITableViewCell: 0xe24afa0; frame = (0 300; 320 75); autoresize = W; layer = <CALayer: 0xe24aee0>>"

and with this

有了这个

UITableViewCell * lastObjectOfArray=arrayOfVisibleTVC.lastObject;
int indexpathLastObject= (lastObjectOfArray.frame.origin.y/new.frame.size.height);

I know the last of arrayOfVisibleTVC.lastObject that I use to get indexpath. but I think there is an another way to get indexpath of TableViewcell that showed.. any one can help me?

我知道用于获取索引路径的最后一个 arrayOfVisibleTVC.lastObject。但我认为还有另一种方法可以获得 TableViewcell 的 indexpath 显示..有人可以帮助我吗?

so I can get indexpath of cell as integer

所以我可以将单元格的索引路径作为整数

回答by Ramis

Here is method in UITableView class:

这是 UITableView 类中的方法:

objective-c

目标-c

- (NSArray *)indexPathsForVisibleRows

Swift

迅速

public var indexPathsForVisibleRows: [NSIndexPath]? { get }

回答by sosborn

UITableView has a method to find the indexPath of a cell:

UITableView 有一个方法来查找单元格的 indexPath:

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell

Just iterate over your visible cells array and pass each cell to get the index path.

只需遍历可见单元格数组并传递每个单元格即可获取索引路径。

for (UITableViewCell *cell in arrayOfVisibleTVC) {
  NSIndexPath *path = [[BNUtilitiesQuick ListController].tableViewA indexPathForCell:cell];
  NSLog(@"%@", path);
}