UICollectionViewCell单选

UICollectionViewCell单选

伙计们可以用uicollectionview引导我,我将边框颜色设置为单元格,现在我的要求是当我点击单元格时边框颜色将是红色,现在我在第二个单元格上点击现在第二个单元格边框将是红色,第一个单元格边框将是clearcolor。

UICollectionViewCell *selectedCell =
[collectionView cellForItemAtIndexPath:indexPath];

selectedCell.contentView.backgroundColor = nil;
[selectedCell.contentView.layer setBorderColor:[UIColor clearColor].CGColor];
[selectedCell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[selectedCell.contentView.layer setBorderWidth:3.0f];

const NSTimeInterval kAnimationDuration = 0.20;
[UIView animateWithDuration:kAnimationDuration animations:^{
    [selectedCell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
    selectedCell.alpha = 0.0f;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:kAnimationDuration animations:^{
        [selectedCell.contentView.layer setBorderColor:[UIColor clearColor].CGColor];
        selectedCell.alpha = 1.0f;
    }];
}];

最佳答案

我找到了解决方案。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *selectedCell =
[collectionView cellForItemAtIndexPath:indexPath];

selectedCell.contentView.backgroundColor = nil;
[selectedCell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[selectedCell.contentView.layer setBorderWidth:3.0f];
}

并添加此委托方法
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *deselectedCell =
[collectionView cellForItemAtIndexPath:indexPath];
deselectedCell.contentView.backgroundColor = nil;
[deselectedCell.contentView.layer setBorderColor:[UIColor clearColor].CGColor];
[deselectedCell.contentView.layer setBorderWidth:3.0f];
}

关于ios - UICollectionViewCell单选,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26906569/

10-10 09:03