【问题标题】:Change generic class type into subclass将泛型类类型更改为子类
【发布时间】:2017-05-28 20:17:12
【问题描述】:

我有这个方法:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        CGPoint p = [gestureRecognizer locationInView:self.collectionView];
        NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
        if (indexPath == nil){
            NSLog(@"couldn't find index path");
        } else {
            UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
                        if ([cell isKindOfClass:[MWGradeCell class]]){
                NSLog(@"Yes");
            //here I would like to get a custom property "cell.gradeLabel.text" that is specific to MWGradeCell
            } else{
               NSLog(@"No");
            }
        }

    } else{
        NSLog(@"ended");
    }

}

它识别 UICollectionview 的哪个 UICollectionviewcell 被长按。 我的 UICollectionView 是用不同类型的子类 UICollectionViewCells 构建的,每个子类都有不同的属性。

现在,我只想获取特定单元格类型的属性,但为了做到这一点,我需要将已识别的 UICollectionViewCell 更改为 MWGradeCell。

不知道怎么做。幸运的是,你们在身边

【问题讨论】:

    标签: ios uicollectionviewcell


    【解决方案1】:

    我不确定我是否理解这个问题,但你可以像这样投射单元格:

    MWGradeCell* cell = (MWGradeCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    

    不记得(MWGradeCell *)是否真的有必要

    【讨论】:

    • 谢谢,我做到了。但是该应用程序在不同的课程上崩溃了。然后我尝试了上面的代码。我应该结合这两种方法。是的, (MWGradeCell *) 是必需的,否则 cellforitematindexpath 将不起作用。
    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多