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

use*_*951 27 objective-c uitableview ios

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

会显示

"<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>>"
Run Code Online (Sandbox Code Playgroud)

并与此

UITableViewCell * lastObjectOfArray=arrayOfVisibleTVC.lastObject;
int indexpathLastObject= (lastObjectOfArray.frame.origin.y/new.frame.size.height);
Run Code Online (Sandbox Code Playgroud)

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

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

Ram*_*mis 52

这是UITableView类中的方法:

Objective-C的

- (NSArray *)indexPathsForVisibleRows
Run Code Online (Sandbox Code Playgroud)

迅速

public var indexPathsForVisibleRows: [NSIndexPath]? { get }
Run Code Online (Sandbox Code Playgroud)


sos*_*orn 9

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

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
Run Code Online (Sandbox Code Playgroud)

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

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

  • 对于那些可以更容易的事情进行大量编码的方法,请参阅Ramis的回答 (2认同)