将通用 class 类型更改为子 class

Change generic class type into subclass

我有这个方法:

-(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。

不知道怎么做。还好有你们在身边

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

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

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