ios - UICollectionView 委托(delegate)方法 "didSelectItemAtIndexPath"在 UITableviewCell 中使用 UICollection View 时未调用

标签 ios objective-c uitableview uicollectionview uicollectionviewcell

我在我的 UITableviewCell 中使用了 UICollectionView,一切正常,仍在加载 Collection View 并显示其单元格,但是在加载 Collection View 后,当点击 Collection View 单元格时,我无法获得 Collection View 的“didSelectItemAtIndexPath”委托(delegate)方法。

我的表格 View 具有静态单元格,我将 Collection View 放在该单元格中

还要在其上定义委托(delegate)和数据源

这是我的代码,调用了数据源方法但未调用委托(delegate)

#pragma mark - collectionView data source


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collCell" forIndexPath:indexPath];
    UIImageView *imgDocCell = (UIImageView *)[cell viewWithTag:100];
    imgDocCell.image = self.imgFileToChart;
    cell.layer.borderWidth=1.0f;
    cell.layer.borderColor=commonThemeColor.CGColor;
    return cell;
}


#pragma mark - collectionView delegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
}

最佳答案

删除 collectioview 和 delegate 和 datasource 的导出,见 OUTPUT HERE

你应该这样写你的代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;   
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    // Here tableviewcell inside one collection view create with tag 10
    UICollectionView *coll =(UICollectionView*)[cell viewWithTag:10];
    coll.delegate = self;
    coll.dataSource = self;

    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
    // in collectionview storyboard one UIImageview with tag 20
    UIImageView *imgDocCell = (UIImageView *)[cell1 viewWithTag:20];
    imgDocCell.image =[UIImage imageNamed:@"add.jpeg"];

    return cell1;
}

#pragma mark - collectionView delegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
}

关于ios - UICollectionView 委托(delegate)方法 "didSelectItemAtIndexPath"在 UITableviewCell 中使用 UICollection View 时未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40013659/

相关文章:

ios - 编译时信息在运行时是否可用?

ios - 使用部分页脚 View 向页脚 View 所在的部分添加一行?

ios - 可扩展 TableVIew 的示例项目

objective-c - Objective-C 中的变量作用域

ios - 在 UITableviewCell 中选择行时遇到问题

ios - 以编程方式将单元格添加到 UITableView

ios - 我是否需要拥有 Apple 开发者帐户才能实现与 Game Center 的交互?

ios - 如何平铺图像

ios - 如何在物镜C中使用colorLiteral,imageLiteral

objective-c - 将其他正在运行的应用程序的窗口置于最前面