1

我在UITableViewCell內有UICollectionView。當我選擇一個tableView行時,我不希望調用UICollectionViewdidSelectItemAtIndexPath:方法。相反,我想didSelectRowAtIndexPath:方法UITableView被調用。如何在didSelectItemAtIndexPath:中撥打didSelectRowAtIndexPath:在UICollectionView選擇上選擇UITableView

+0

首先嚐試make collectionview選擇無 – Vvk

+0

集合視圖的單元格是否可選?我的意思是你需要收集視圖的這些單元格可選 –

+0

我不希望它們可選,但我希望它們可以滾動。我試圖禁用collectionView的userInteraction。在選擇時,'didSelectRowAtIndexPath:'最終被調用,但是,collectionView不滾動 – iOS

回答

1

設置collectionView.allowsSelection = NO禁用選擇。

didSelectItemAtIndexPath方法,抓住的UITableViewCell和調用該委託其爲具有的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 
{ 

} 
+0

上面的解決方案沒有工作:( – iOS

+0

怎麼來的,它應該禁用選擇收集視圖。你仍然在接收'didSelectItemAtIndexPath:'? –

+0

檢查我更新的解決方案 –

1

夫特3

首先設置以下

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let parentCell: QMatrixCell? = collectionView.superview?.superview as! QMatrixCell? 
    parentCell?.delegate.collectionViewDidSelectedAtCell(cell: parentCell!) 
} 

並在包含您的tableview的控制器中執行以下操作

extension TableViewController: SelectionCell{ 
func collectionViewDidSelectedAtCell(cell : QMatrixCell){ 
    let index: IndexPath? = tableView.indexPath(for: cell) 
    tableView(tableView, didSelectRowAt: index!); 
} 
} 
protocol SelectionCell { 
    func collectionViewDidSelectedAtCell(cell : QMatrixCell); 
} 

不要忘記在你的tableView單元格中設置委託。