男人们可以用uicollectionview引导我,我把边框颜色设置为单元格,现在我的要求是,当我点击单元格时,边框颜色将是红色,现在我正在点击第二个单元格,第二个单元格边框将是红色,第一个单元格边框将是透明颜色。
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;
}];
}];
发布于 2014-11-13 10:47:05
我找到解决办法了。
- (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];
}
发布于 2014-11-13 10:47:21
使用didSelectItemAtIndexPath并保存选定单元格的IndexPath。这是我的代码,我正在使用与您要求的相同的东西。只是代替..。
- (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;
}
https://stackoverflow.com/questions/26906569
复制相似问题