无法获取UICollectionViewCell的正确索引

时间:2015-12-12 02:25:03

标签: ios objective-c uicollectionview uicollectionviewcell

这里是背景:我有一个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:委托中添加了一个通知海报,让主控制器知道该选择来自哪个集合视图单元。

这是我问题的整个过程。

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

  • 从UIScrollView的委托中获取索引;
  • 使用indexPathForItemAtPoint:或类似方法,按您触摸的点获取索引。
  • 有人说它应该是collectionview中的 indexPath.item 而不是 indexPath.row ,所以我记录了它们并没有看到差异。

所以我的问题是:

  • 是否有直接获取UICollectionView的正确索引的方法?为什么我按[_questions objectAtIndex:indexPath.item]获取正确的模型,但indexPath.item的错误索引?

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

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

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

3 个答案:

答案 0 :(得分:0)

我给你样本集合视图代码。它可以帮助你获得索引。

在viewDidLoad

 UINib *cellNib = [UINib nibWithNibName:@"QuestionCVCell" bundle:nil];
 [collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
数据源方法

中的

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

答案 1 :(得分:0)

而不是使用indexPath.row在UICollectionView中使用indexPath.item。它会给你正确的索引。

NSIndexpath.item vs NSIndexpath.row

答案 2 :(得分:0)

使用委托模式来实现此目的。

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类中使用集合视图索引调用tableSelectedWithIndex方法