How to fix an Incompatible pointer for subclass of UICollectionViewCell

耗尽温柔 提交于 2019-12-11 13:17:42

问题


I've implemented a subclass SPCell of UICollectionViewCell which I am using to create cells in my UICollectionViewController. Now, when I gather a cell, in order to do something with it, I get a warning Incompatible pointer types initializing 'SPCell *' with an expression of type 'UICollectionViewCell *'

Here is an example. My custom cell holds an image, which I want to change the alpha value. But in the line, where I assign the cell, I get this warning.

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;{  
    SPCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    cell.thumbnail.alpha=0.6;
}

Thanks in advance.


回答1:


You just need to add a cast to let the compiler know what you are doing -

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;{  
    SPCell *cell = (SPCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    cell.thumbnail.alpha=0.6;
}


来源:https://stackoverflow.com/questions/23578412/how-to-fix-an-incompatible-pointer-for-subclass-of-uicollectionviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!