UICollectionViewCell在选择时使字体加粗,在取消选择时使其规则-内存占用



选择单元格时,子类方法会覆盖setSelected:

查看控制器代码

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

CollectionView子类方法

-(void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if(self.isSelected){

        self.myLabel.font = [UIFont boldSystemFontOfSize:10.0];
    }else{
        self.myLabel.font = [UIFont systemFontOfSize:10.0];
    }
}

我也在尝试更改文本颜色。那么,是不是每次我选择新的UIFont对象都会被创建,如果设置了文本颜色(一些自定义UIColor),也会创建新的对象。或者有更好的方法吗?

视图控制器代码是多余的。系统将自动完成所有这些操作。

关于创建UIFont对象,开销是最小的,您不需要担心这一点。

最新更新