2011-11-30 69 views
26
NSArray * arrayOfVisibleTVC=[BNUtilitiesQuick ListController].tableViewA.visibleCells; 
NSLog(@"array : %@", arrayOfVisibleTVC); 

将显示如何知道UITableView.visibleCells的indexpath?

"<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>>" 

以及与此

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

我知道上次arrayOfVisibleTVC.lastObject的,我用得到indexpath。但我认为有另一种方式来获得TableViewcell的indexpath,显示..任何人都可以帮助我?

这样我就可以得到电池的indexpath作为整数

+3

你尝试*** indexPathForCell:** * 方法? – EmptyStack

回答

49

这里是方法的UITableView类:

Objective-C的

- (NSArray *)indexPathsForVisibleRows 

斯威夫特

public var indexPathsForVisibleRows: [NSIndexPath]? { get } 
+1

这似乎是一个更好的答案,除非我错了。 –

+1

@Ramis除非您使用可见行索引路径来添加和删除行,这会导致在错误的索引处添加或删除行,否则可以这么做。 –

+0

@AshishPisey问题是关于可见行,答案是关于可见行。 – Ramis

9

的UITableView必须找到一个细胞的indexPath方法:

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

只是遍历你可见细胞阵列,并通过每一个单元以获得索引路径。

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

对于可以更容易的事情的代码的方法,请参阅Ramis回答 –