首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionViewCell单选

UICollectionViewCell单选
EN

Stack Overflow用户
提问于 2014-11-13 10:25:16
回答 2查看 1.6K关注 0票数 1

男人们可以用uicollectionview引导我,我把边框颜色设置为单元格,现在我的要求是,当我点击单元格时,边框颜色将是红色,现在我正在点击第二个单元格,第二个单元格边框将是红色,第一个单元格边框将是透明颜色。

代码语言:javascript
运行
复制
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;
    }];
}];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-13 10:47:05

我找到解决办法了。

代码语言:javascript
运行
复制
- (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];   
}

并添加此委托方法。

代码语言:javascript
运行
复制
- (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];
}
票数 2
EN

Stack Overflow用户

发布于 2014-11-13 10:47:21

使用didSelectItemAtIndexPath并保存选定单元格的IndexPath。这是我的代码,我正在使用与您要求的相同的东西。只是代替..。

代码语言:javascript
运行
复制
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ProductCell *selectedCell = (ProductCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [selectedCell setBackgroundColor:[UIColor redColor]];

    if (self.selectedIndexPath)
    {
        ProductCell *deselectedCell = (ProductCell *)[collectionView cellForItemAtIndexPath:self.selectedIndexPath];
        [deselectedCell setBackgroundColor:[UIColor clearColor]];
    }

    self.selectedIndexPath = indexPath;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26906569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档