2016-11-20 159 views
0

我有一个表视图,此方法找到它的细胞之一的子视图的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 ...

enter image description here

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 
} 
+1

单元格当前是否在表格视图中可见?如果单元格可见,则只能获得索引路径。 – rmaddy

+0

谁在调用'indexPathContainingView'和什么时候? – shallowThought

+0

@rmaddy - 是的,该单元格是未滚动表格中的第一个单元格。 – user1272965

回答

1

假设你使用的是故事板,如果你愿意改变你使用的是可能的解决办法是方法(在我看来一个清洁的解决方案) :

将您单元格内的UICollectionView的插口拖入UITableViewCell。在您的故事板中,再次通过拖动插座将UICollectionViewdelegatedataSource设置为您的课程。当然,你将不得不使用定制UITableViewCell

在你cellForRowAtIndexPathUITableView设置的UICollectionView的标签是你indexPath.row。现在在您的numberOfItemsInSection中,您可以访问UICollectionView标记属性,它将包含您尝试访问的UITableViewCellindexPath.row,因此您可以访问您的模型。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 

    // do stuff to setup the cell 
    // now reload the collection view that it contains 
    cell.collectionView.tag = indexPath.row; 
    [cell.collectionView reloadData]; 
    return cell; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 

// get my model from the index path and return the count of an array it contains 
    Model *model = dataArray[collectionView.tag]; 

    return model.count; 

} 
+0

谢谢,我明白了。你能提出一个原因,为什么我的方法不起作用吗?如果我的方法奏效了,我认为它会比这个建议更好(尽管我很感谢你的帮助!!),因为你的想法依赖于单元格的自定义类和(更重要的)滥用'tag'属性的类确实有其他意义。考虑如果我在表格中也有部分,我会将标记整数屏蔽到一个部分和一行吗? – user1272965

+0

但我很欣赏这个想法。如果我无法弄清楚,我可能会被卡住。我当然希望@rmaddy可以在这里提供一些观点。 – user1272965

+0

你可以简单地通过继承UICollectionView来包含两个属性来传递段和行。当然,我会看看!我对你的方法有一些疑问:D。在你的'indexPathContainingView'的第一行。你写了'[UITableViewCell self]'而不是'[UITableViewCell class]'? 并检查什么是'view.superView'出来是什么?它不是单元的'contentView'而不是单元本身?尝试使用'view.superView.superView'! – Rikh