UICollectionViewCell点击高亮完美解决方案

#pragma mark - collectionView点击高亮- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView cell...
摘要由CSDN通过智能技术生成
#pragma mark - collectionViewCell点击高亮
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = kRGBColor(238, 238, 238);

}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) cell?.imageView?.image = UIImage(named: "highlightedImage") } ### 回答2: 在 Swift 中,可以使用 `UITableViewDelegate` 协议的 `didSelectRowAt` 方法来实现点击 UITableViewCell 进行高亮图片的效果。以下是一个示例代码: 首先,在你的视图控制器中设置 `UITableViewDelegate`: ```swift class ViewController: UIViewController, UITableViewDelegate { // ... } ``` 在 `viewDidLoad` 方法中,将 `UITableViewDelegate` 设置为你的 `UITableView` 的代理: ```swift override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self } ``` 然后,实现 `didSelectRowAt` 方法来处理选中 cell 的操作,可以在这个方法中获取 cell 的索引并更改其状态: ```swift func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let cell = tableView.cellForRow(at: indexPath) else { return } // 选中时的高亮效果,可以自行修改 cell.contentView.backgroundColor = .yellow } ``` 最后,为了取消高亮效果,还需要实现 `didDeselectRowAt` 方法: ```swift func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { guard let cell = tableView.cellForRow(at: indexPath) else { return } // 取消高亮效果 cell.contentView.backgroundColor = .white } ``` 以上是一个点击 UITableViewCell 高亮图片的 Swift 代码示例,你可以根据自己的需求修改高亮效果的样式和图片。 ### 回答3: 下面是一个 Swift 的代码示例,用于在点击表格单元格时高亮显示图片。 ```swift import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { let cellID = "cellIdentifier" let colors = [UIColor.red, UIColor.green, UIColor.blue] var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView = UITableView(frame: self.view.frame) tableView.delegate = self tableView.dataSource = self tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID) self.view.addSubview(tableView) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return colors.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) cell.selectionStyle = .none cell.textLabel?.text = "Row \(indexPath.row)" cell.textLabel?.textColor = UIColor.white cell.backgroundColor = colors[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) cell?.imageView?.highlightedImage = UIImage(named: "highlighted_image") cell?.setHighlighted(true, animated: true) } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) cell?.imageView?.highlightedImage = nil cell?.setHighlighted(false, animated: true) } } ``` 上述代码中,我们创建了一个包含了不同颜色单元格的表格视图。当点击单元格时,我们设置`imageView`的`highlightedImage`为一个高亮图片,并将单元格的高亮状态设置为`true`。当取消选中单元格时,我们将`highlightedImage`设置为`nil`,并将单元格的高亮状态设置为`false`。请确保你有一个名为`highlighted_image`的图片文件,且存在于项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值