ios - 单元格上的 UicollectionViewCell 选择更改边框颜色

标签 ios objective-c uicollectionview

我有一个自定义的 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/

相关文章:

ios - 如何使 collectionview 响应其自身 View 之外的平移手势

ios - 解析 - 重新发送电子邮件验证

ios - Facebook SDK iOS 邀请没有 FBWebDialogs 的用户

iphone - 取消alertview的功能

swift - Collection View 单元格不更改图像

iphone - 第一次单元格可见时未绘制 UIImageView 阴影

ios - 使用 Apple 的 Testflight 安装旧版本

ios - 防止索引越界

ios - AFNetworking 2.0 将 NSURLSessionDataTask 转换为 NSURLSessionDownloadTask 不会将所有文件数据写入磁盘

objective-c - 子类 NSViewController 或 NSView