【问题标题】:Failed to get the correct index for UICollectionViewCell未能获取 UICollectionViewCell 的正确索引
【发布时间】:2016-03-18 01:48:33
【问题描述】:

这是背景:我有一个 UICollectionView,其中包含 UITableView 作为其单元格。每当我选择 UITableViewCell 时,我都想知道该操作来自哪个 UICollectionViewCell

但是我没有得到正确的 UICollectionViewCell 索引。如下代码。

首先,在 init 方法中,我注册了我的自定义单元格。

 [self registerClass:[QuestionCVCell class] forCellWithReuseIdentifier:QuestionCVCellIdentifier];

然后,在 UICollectionViewDataSource 中。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
        cell = (QuestionCVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:QuestionCVCellIdentifier forIndexPath:indexPath];
        if (_questions && _questions.count > 0) {
            ((QuestionCVCell *)cell).model = [_questions objectAtIndex:indexPath.item];
            ((QuestionCVCell *)cell).index = indexPath.item;

            NSLog(@"   %ld, %ld",(long)indexPath.item, (long)indexPath.row);
        }
       return cell;
}

日志乱七八糟,和我说的 right 索引不一样。我猜这和dequeueReusableCellWithReuseIdentifier有关?

在上面的代码中,cell.index 被传递给QuestionCVCell,其中包含一个tableview。我在其didSelectRowAtIndexPath: 委托中添加了一个通知海报,让主控制器知道选择来自哪个collectionview 单元格。

这就是我的问题的全过程。

我在 Google 中搜索过类似的问题,并找到了一些类似的解决方案:

  • 从 UIScrollView 的委托中获取索引;
  • 通过触摸点获取索引,使用indexPathForItemAtPoint: 或类似方法。
  • 有人说应该是collectionview中的indexPath.item,而不是indexPath.row,所以我把它们都记录了,没看出区别。李>

所以我的问题是:

  • 有没有直接获取UICollectionView的正确索引的方法?为什么[_questions objectAtIndex:indexPath.item] 得到了正确的模型,而indexPath.item 得到了错误的索引?

  • 有没有更好的方法让主控制器知道tableview单元格选择动作属于哪个collectionview单元格?

  • 如果您在我的解决方案中发现任何问题或错误,请告诉我。

如果有人可以提供帮助,请提前致谢。

【问题讨论】:

  • 根本不清楚您在问什么或您的问题是什么。 indexPath.item 是显示的代码需要为其返回单元格的项目。您是否尝试将didSelectRowAtIndexPath 中的这些信息用于您的嵌入式表格视图?如果是,则显示该代码。
  • @Paulw11 很抱歉描述不清楚。简而言之,我的问题是当我水平滚动 UICollectionView 时无法获得正确的索引。这是我的demo。谢谢你的耐心。 :)
  • 我仍然不知道您所说的“正确”是什么意思 - 提供给您在 indexPath.item 中显示的代码的 indexPath.item 是代码返回的单元格所必需的你已经展示了;它本质上是“正确的”;如果您指的是在其他函数中访问 indexPath,请显示该代码。
  • @Paulw11 我刚刚更新了我的问题。请看一看。
  • 我还是不明白哪里你弄错了indexPath.item哪行代码?你得到什么价值?你期望什么价值?

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

我给你示例集合视图代码。它可以帮助你获取索引。

在 viewDidLoad 中

 UINib *cellNib = [UINib nibWithNibName:@"QuestionCVCell" bundle:nil];
 [collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];

在 CollectionView 数据源方法中

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *cellIdentifier = @"cvCell";
 QuestionCVCell *cell = (QuestionCVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
 //Here you need to give your QuestionCVCell data instead of my data;
 cell.img_Collection.image = [imgArray objectAtIndex:indexPath.row];
 cell.lbl_Collection.text = [lblArray objectAtIndex:indexPath.row];
 NSLog(@"The current indexPath row is - %ld",(long)indexPath.row);
 cell.tag = indexPath.row;
 return cell;
}

在集合视图委托方法中

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"The touched index path os collection cell item row is - %ld",(long)indexPath.row);
}

【讨论】:

    【解决方案2】:

    在 UICollectionView 中使用 indexPath.item 代替 indexPath.row。它会给你正确的索引。

    NSIndexpath.item vs NSIndexpath.row

    【讨论】:

    • 我看不出 indexpath.row 和 indexpath.item 有什么区别
    【解决方案3】:

    使用委托模式来实现这一点。

    i) 在 CollectionView 数据源方法中将单元格标记设置为 indexPath.row

     - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        QuestionCVCell *cell = (QuestionCVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:QuestionCVCellIdentifier forIndexPath:indexPath];
    
        if (_questions && _questions.count > 0) {
            cell.model = [_questions objectAtIndex:indexPath.item];
        }
        cell.delegate = self;
        cell.tag = indexPath.row;
        return cell;
    }
    

    ii) 在 QuestionCVCell 类中创建自定义委托

    @protocol QuestionCVCellDelagate <NSObject>
    - (void)tableSelectedWithIndex:(NSUInteger)index;
    @end
    
    @interface QuestionCVCell : UICollectionViewCell
    @property (nonatomic, weak) id<QuestionCVCellDelagate>delegate;
    @end
    

    iii) 在 tableView didSelectRowAtIndexPath 方法中调用委托方法

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (self.delegate && [self.delegate respondsToSelector:@selector(tableSelectedWithIndex:)]) {
            [self.delegate tableSelectedWithIndex:self.tag];
        }
    }
    

    iii) 采用 ViewController 类中的协议并添加委托方法。

    - (void)tableSelectedWithIndex:(NSUInteger)index {
        //do whatever you with the index.
    }
    

    现在只要选择表行,它会在ViewController类中使用Collection View索引调用TableSelectedWithIndex方法

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      相关资源
      最近更新 更多