【问题标题】:Select the UITableView on UICollectionView selection在 UICollectionView 选择上选择 UITableView
【发布时间】:2016-02-17 05:53:09
【问题描述】:

我在UITableViewCell 中有一个UICollectionView。当我选择 tableView 行时,我不希望调用 UICollectionView 的 didSelectItemAtIndexPath: 方法。相反,我希望 UITableView 的 didSelectRowAtIndexPath: 方法被调用。我如何在didSelectItemAtIndexPath: 中调用didSelectRowAtIndexPath:?

【问题讨论】:

  • 请先尝试使collectionview selection none
  • 集合视图的单元格可以选择吗?我的意思是你需要选择这些集合视图的单元格
  • 我不希望它们可以选择,但我希望它们可以滚动。我尝试禁用collectionView的userInteraction。在选择时,最终会调用 didSelectRowAtIndexPath:,但是,collectionView 没有滚动
  • @iOS : 如果你禁用 collectionView 的用户交互,它将停止滚动。那时不可能与集合视图进行任何类型的交互。所以禁用用户交互不是一种选择。
  • @PulkitSharma :这是一次尝试。

标签: ios uitableview uicollectionview didselectrowatindexpath


【解决方案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 单元格中设置委托。

【讨论】:

    【解决方案2】:

    设置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
    {
    
    }
    

    【讨论】:

    • 怎么会,它应该禁用集合视图的选择。你还在接收didSelectItemAtIndexPath: 吗?
    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2013-02-26
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多