【发布时间】:2016-11-20 15:58:21
【问题描述】:
我有一个表格视图和这个方法来查找其中一个单元格的子视图的 indexPath...
- (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
}
【问题讨论】:
-
当前单元格在表格视图中是否可见?如果单元格可见,您只会获得索引路径。
-
谁在打电话给
indexPathContainingView,什么时候打电话? -
@rmaddy - 是的,该单元格是未滚动表格中的第一个单元格。
-
@shallowThought 在单元格包含的集合视图的数据源方法中,它是从同一个 vc 的其他地方调用的。要获得该集合视图的计数,我需要知道索引路径(这样我才能在表视图的数据源中查找正确的内容)。
-
并且单元格已从表格视图中正确出列?
标签: ios objective-c uitableview