4
votes

In short: how can I prevent the selection of a collection view cell from affecting the selection state of table view cells that are inside that collection view cell?

I have a UICollectionView, where the cells are displayed full-screen when selected. The cells contain a UITableView that will display use information.

The problem I'm having is that the cells in the table view are being displayed as if they are selected. I moved some things around, to display a cell full-screen all the time, which allowed me to confirm: when the collection view cell is selected, the cells of the table view inside of it are also displayed with the selected style.

Note that I say they are displayed with the selected style, and not they are selected. As far as the table view is concerned, they are not selected.

[tableView indexPathsForSelectedRows]; returns no index paths. tableView:willSelectRowAtIndexPath:—from which I could return nil—is never called. So, it's safe to say that the table view doesn't perceive its own cells as being selected.

But, I can confirm in Reveal that the table view cells contain the selected background view (see the last screenshot below). This is only the case for table view cells that are visible when the collection view cell is selected: this does not happen to cells that are far enough down in the list to be off-screen initially.

I can hide the problem using cell.selectionStyle = UITableViewCellSelectionStyleNone; and ultimately, this might be my solution: the table view cells will not be selectable anyway, so painting over the problem like this should work in my situation.

But, that doesn't answer the bigger question: what the heck is going on here?

Screenshots

Normal collection view cell (this whole thing is one cell):

Normal cell view

Selected collection view cell, with gray selected style on the table view within:

Selected cell

Reveal, showing the selected background view. In this case, the table cell containing the Delete button was not visible when the collection view cell was selected, so it didn't get the selection styling like the cells above it did:

Selected Background view

2
Definitely the same problem, but I found the chosen solution to be buggy in its own way. I opened two radars about these. This issue, with recursive highlighting: openradar.me/radar?id=5888315068776448 and the other question's answer, with shouldHighlight: preventing cell selection: openradar.me/radar?id=5803034198147072 - Brock Boland

2 Answers

2
votes

I've overridden UITableViewCell's method this way

func setHighlighted(_ highlighted: Bool, animated: Bool)
{
   //Leave Empty
}

Apple behaviour could be right, because in this way you could skip an iteration on all child and set manually the property, but why I can't choice if i wan't it using UICollectionViewDelegate method?

func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool
1
votes

Apparently, this is the expected behavior in iOS 7. setHighlighted: is called on all subviews that support it.