问题
I have a UICollectionView
inside UITableViewCell
. When I select the a tableView
row, I do not want the didSelectItemAtIndexPath:
method of UICollectionView
to get called. Instead I want didSelectRowAtIndexPath:
method of UITableView
to get called. How would I call didSelectRowAtIndexPath:
in didSelectItemAtIndexPath:
?
回答1:
Set collectionView.allowsSelection = NO
to disable selection.
OR in the didSelectItemAtIndexPath
method, grab the UITableViewCell and call the delegate which is the table view that has UITableView:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *parentCell = collectionView.superview;
[parentCell.delegate collectionViewDidSelectedAtCell:parentCell];
}
// IN your view controller that has the table view
- (void)collectionViewDidSelectedAtCell:(UITableViewCell *)cell{
NSIndexPath *index = [self.tableView indexPathForCell:cell];
[self tableView:self.tableView didSelectRowAtIndexPath:index];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
回答2:
Swift 3
First set the following
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let parentCell: QMatrixCell? = collectionView.superview?.superview as! QMatrixCell?
parentCell?.delegate.collectionViewDidSelectedAtCell(cell: parentCell!)
}
and in the controller that contains your tableview do the following
extension TableViewController: SelectionCell{
func collectionViewDidSelectedAtCell(cell : QMatrixCell){
let index: IndexPath? = tableView.indexPath(for: cell)
tableView(tableView, didSelectRowAt: index!);
}
}
protocol SelectionCell {
func collectionViewDidSelectedAtCell(cell : QMatrixCell);
}
and don't forget to set delegate in your tableView cell.
来源:https://stackoverflow.com/questions/35448927/select-the-uitableview-on-uicollectionview-selection