【问题标题】:indexPathForCell: returning nilindexPathForCell:返回零
【发布时间】: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


【解决方案1】:

假设您正在使用情节提要,并且如果您愿意改变方法,那么您正在使用的可能解决方案将是(并且我认为是更清洁的解决方案):

将单元格内的UICollectionView 的出口拖到UITableViewCell 中。在您的故事板中,通过拖动插座再次将UICollectionViewdelegatedataSource 设置为您的班级。当然你必须使用自定义的UITableViewCell

在您的cellForRowAtIndexPath 中为UITableView 设置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;

}

【讨论】:

  • 谢谢,我明白了。你能提出我的方法不起作用的原因吗?如果我的方法有效,我认为它会比这个建议更好(感谢您的帮助!!),因为您的想法依赖于单元格的自定义类,并且(更重要的是)有点滥用 tag 属性确实有别的意思。考虑一下如果我在表中也有部分,我会将标记整数屏蔽为一个部分和一行吗?
  • 但我很欣赏这个想法。如果我无法弄清楚,我可能会被困住。我当然希望@rmaddy 可以在这里提供一些观点。
  • 你可以简单地通过继承UICollectionView 来包含两个属性来传递节和行。当然我会看看!我对您的方法有一些疑问:D。在您的indexPathContainingView 的第一行。你写了[UITableViewCell self],不应该是[UITableViewCell class]吗?还要检查一下view.superView 是什么?它不会是单元格的contentView 而不是单元格本身吗?尝试使用view.superView.superView!
  • :-) 谢谢!将selfclass 发送到一个类是同义词。你是对的,在从子视图到单元格的过程中可能会有一些中间超级视图,但我在循环中运行它,直到我到达单元格。查看 lldb 输出以证明我已找到该单元格并且该单元格属于该表。
  • 哦,我不知道selfclass 部分!那么我能想到的唯一可能导致问题的其他可能原因是使用与您传递的参数相同的变量名来分配view.superview!但除此之外,我无法弄清楚为什么您的代码不起作用!对不起!
猜你喜欢
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 2016-11-24
  • 2021-10-24
相关资源
最近更新 更多