简体   繁体   English

在UITableviewCell中使用UICollection视图时,未调用UICollectionView委托方法“ didSelectItemAtIndexPath”

[英]UICollectionView delegate method “didSelectItemAtIndexPath” not called when UICollection view used in UITableviewCell

I used UICollectionView in my UITableviewCell, Everything is working fine still loading the collection view and display its cell, But after loading collection view, i unable to get "didSelectItemAtIndexPath" delegate method of collection view when tap on collection view cell. 我在UITableviewCell中使用了UICollectionView,仍然可以正常运行,仍然加载了集合视图并显示其单元格,但是在加载了集合视图之后,当点击集合视图单元格时,我无法获得集合视图的“ didSelectItemAtIndexPath”委托方法。

My Table view having static cell and i put collection view in that cell 我的表格视图具有静态单元格,我在该单元格中放置了集合视图

Also define the delegate and datasource on it 还定义委托和数据源

Here is my code, data source method called but delegate not called 这是我的代码,调用了数据源方法,但未调用委托

#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);
}

Remove outlet of collectioview and delegate and datasource too, See OUTPUT HERE 也要删除collectioview的出口以及委托和数据源,请参阅此处的输出

you should write your code like this 你应该这样写你的代码

- (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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView:在使用其他手势后未调用didSelectItemAtIndexPath - UICollectionView: didSelectItemAtIndexPath not called after other gestures are used UICollectionView的didSelectItemAtIndexPath方法仅被调用一次 - UICollectionView's didSelectItemAtIndexPath method is called only once UICollectionView didSelectitematIndexPath委托吗? - UICollectionView didselectitematindexpath delegate? 在 UITextView 上点击时未调用 UICollectionView didSelectItemAtIndexPath - UICollectionView didSelectItemAtIndexPath not called when tapped on UITextView 即使设置了委托,UICollectionView didSelectItemAtIndexPath也不会调用 - UICollectionView didSelectItemAtIndexPath never called even though delegate is set 未调用 UICollectionView 的 didSelectItemAtIndexPath - didSelectItemAtIndexPath of UICollectionView is not called didSelectItemAtIndexPath方法不在UICollectionView中工作 - didSelectItemAtIndexPath method not working in a UICollectionView UICollectionView的didSelectItemAtIndexPath仅在用两根手指选择单元格时调用 - UICollectionView's didSelectItemAtIndexPath only called when selecting cell with two fingers 如何防止collectionView:didSelectItemAtIndexPath委托方法多次调用 - How to prevent collectionView:didSelectItemAtIndexPath delegate method called more than once iOS:向容器视图添加UITapGestureRecognizer拦截UICollectionView的didSelectItemAtIndexPath方法 - iOS: Adding UITapGestureRecognizer to container view intercepts UICollectionView's didSelectItemAtIndexPath method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM