我有一个自定义的 UICollectionVIewCell
。当用户选择一个单元格时,我希望单元格的边框颜色为红色,而当用户取消选择它时,我希望清除单元格边框的颜色。
这怎么可能?
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
CustomeCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor=[UIColor redColor];
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
CustomeCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor=[UIColor clearColor];
}
最佳答案
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSString *img =[[images objectAtIndex:indexPath.row]valueForKey:@"imageName"];
cell.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:img]];
cell.layer.borderWidth=10;
cell.layer.borderColor=[UIColor greenColor].CGColor;
return cell;
}
这是我的应用程序中的工作代码。如果您提供 bordercolor,请确保有 borderwidth。
关于ios - 单元格上的 UicollectionViewCell 选择更改边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32498445/