user1272965 user1272965 - 20 days ago 12
Objective-C Question

indexPathForCell: returning nil

I've got a table view and this method to find the indexPath of a subview of one of its cells...

- (NSIndexPath *)indexPathContainingView:(UIView *)view {
while (view && ![view isKindOfClass:[UITableViewCell self]]) {
view = view.superview;
}
UITableViewCell *cell = (UITableViewCell *)view;
return (cell)? [self.tableView indexPathForCell:cell] : nil;
}


I call with a subview of the topmost cell (which is visible, the table view has not been scrolled), and, with a breakpoint on the
return
line, I get this perplexing result in lldb...

enter image description here

cell
looks good. The cell's table view matches my
tableView
(there's some intermediate
UITableViewWrapperView
as the direct superview). But see how
indexPathForCell:
returns nil? I am certain that the cell is visible.

I don't think it matters, but the subview of the
cell
I begin with is a
UICollectionView
, and I'm calling this on the collection view's datasource methods.

Any ideas?

EDIT more context...

- (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
}

Answer

Assuming you are using storyboards and if you are willing to change the approach you are using a possible solution would be (And a cleaner solution in my opinion):

Drag an outlet of your UICollectionView inside your cell into the UITableViewCell. In your storyboard again set the delegate and dataSource of the UICollectionView to your class by dragging the outlet. Of course you will have to use a custom UITableViewCell

In your cellForRowAtIndexPath for the UITableView set the tag of the the UICollectionView to be your indexPath.row. Now in your numberOfItemsInSection you can access the UICollectionView tag property and it will contain the indexPath.row of the UITableViewCell you are trying to access and hence you can access your model.

- (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;

}