UITableView inside UICollectionViewCell, all UITableViewCells are highlighted when a cell is clicked

I create a UICollectionView where some of the UICollectionViewCells contain a UITableView .

This works fine and everything is fine until I touch UICollectionViewCell somewhere else than UITableView . This invokes the setHighlighted method setHighlighted for all UITableViewCells in the table.

Below is a rough drawing of a UICollectionViewCell . UITableView only covers β€œcell one” to β€œcell three”. Clicking anywhere outside this table, but inside the UICollectionViewCell causes the selection of cells.

 ------------------------- | Title goes here | | | ------------------------- | | | Cell one | ------------------------- | | | Cell two | ------------------------- | | | Cell three | ------------------------- | Button outside table | |-----------------------| 

The call stack looks something like this.

 [MyTableViewCell setHighlighted:] [UICellHighlightingSupport highlightView:] UIApplicationMain main 

It seems that the UICollectionViewCell redirects the selection command to all cells.

I worked on the problem by overloading the setHighlighted method in my subclass of UITableViewCell and not calling the super implementation. This seems a bit hacky, though, and I wonder if this behavior can be somehow avoided.

EDIT: I assume this behavior occurs when a UICollectionCellView setHighlighted is called for all of its children. I understand that this is useful in most other cases.

+6
source share
2 answers

Have you tried implementing the following UICollectionViewDelegate method?

 collectionView:shouldHighlightItemAtIndexPath: 

If you return NO for your UITableView in the collection view, then you should be good to go.

+2
source

To solve this problem and to allow the selection of table view cells when connected directly and to avoid overriding the View collection: shouldHighlightItemAtIndexPath: since this prevents selection, I override the UICollectionViewCell setHighlighted method and unselect the one that it performed on my table cells. Thus, table view cells are not displayed if the collection view is selected.

 - (void) setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (highlighted) { dispatch_async(dispatch_get_main_queue(), ^ { for (UITableViewCell* cell in self.tableView.visibleCells) cell.highlighted = NO; }); } } 

I sent the selection because it seems that the UICollectionViewCell is also delaying the selection. I need my highlight to happen after UICollectionViewCell's.

+1
source

Source: https://habr.com/ru/post/956220/


All Articles