Select the UITableView on UICollectionView selection

旧时模样 提交于 2021-01-28 01:47:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!