我有一个表视图,此方法找到它的细胞之一的子视图的indexPath ...indexPathForCell:返回nil
- (NSIndexPath *)indexPathContainingView:(UIView *)view {
while (view && ![view isKindOfClass:[UITableViewCell self]]) {
view = view.superview;
}
UITableViewCell *cell = (UITableViewCell *)view;
return (cell)? [self.tableView indexPathForCell:cell] : nil;
}
我最顶端细胞的一个子视图调用(可见,该表视图有不被滚动),并与在return
线断点,我得到这个令人困惑的结果LLDB ...
cell
看起来不错。单元格的表格视图与我的tableView
相匹配(有一些中级UITableViewWrapperView
作为直接上级视图)。但看看indexPathForCell:
如何返回零?我确信细胞是可见的。
我不认为它很重要,但cell
的子视图是UICollectionView
,我在集合视图的数据源方法上调用它。
任何想法?
编辑更多的背景...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// do stuff to setup the cell
// now reload the collection view that it contains
UICollectionView *collectionView = (UICollectionView *)[cell viewWithTag:33];
[collectionView reloadData];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSIndexPath *indexPath = [self indexPathContainingView:collectionView];
return // get my model from the index path and return the count of an array it contains
// but here's the problem. index path is nil here
}
单元格当前是否在表格视图中可见?如果单元格可见,则只能获得索引路径。 – rmaddy
谁在调用'indexPathContainingView'和什么时候? – shallowThought
@rmaddy - 是的,该单元格是未滚动表格中的第一个单元格。 – user1272965