You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

返回ViewController A时,如何取消UICollectionViewCell的高亮状态?

解决UICollectionViewCell返回页面后高亮状态无法取消的问题

首先咱们理清楚问题根源:你在didHighlightItemAtIndexPath里给cell的contentView设置了高亮背景色,但没有对应的逻辑来取消这个自定义高亮状态——不管是用户手指滑开cell取消触摸,还是跳转到B页面再返回A,这个高亮背景色都不会自动恢复;再加上UICollectionView的cell复用机制,还可能导致其他复用cell也出现异常的高亮状态。

下面给你几个针对性的解决步骤:

1. 补充取消高亮的回调方法

在ViewController A中实现didUnhighlightItemAtIndexPath方法,当cell结束高亮时立即恢复默认背景色:

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    // 替换成你cell原本的背景色,比如白色或主题默认色
    cell.contentView.backgroundColor = [UIColor whiteColor];
}

这个方法会在用户手指离开cell时触发,及时把cell状态拉回正常。

2. 从B页面返回时强制重置高亮状态

当你从Product_ViewController返回当前页面时,之前的cell可能还残留高亮状态,所以在viewWillAppear:里手动检查并清除:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 获取当前处于高亮状态的cell索引
    NSIndexPath *highlightedPath = self.collectionView.indexPathsForHighlightedItems.firstObject;
    if (highlightedPath) {
        // 系统层面取消高亮
        [self.collectionView unhighlightItemAtIndexPath:highlightedPath];
        // 强制恢复背景色,确保状态完全重置
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:highlightedPath];
        if (cell) {
            cell.contentView.backgroundColor = [UIColor whiteColor];
        }
    }
}

3. 修复cell复用导致的状态异常

为了避免复用的cell带着之前的高亮状态显示,在cellForItemAtIndexPath方法中给每个cell设置默认背景色,确保复用cell的初始状态是正常的:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *gridcell = nil;
    if(_aryWishlist.count > 0) {
        WishlistCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WishlistCellID forIndexPath:indexPath];
        
        // 新增:设置cell默认背景色,覆盖复用残留的状态
        cell.contentView.backgroundColor = [UIColor whiteColor];
        
        [cell.sameButton setTitle:[_aryWishlist [indexPath.row]valueForKey:@"condition"] forState:UIControlStateNormal];
        if([[_aryWishlist[indexPath.row]valueForKey:@"price_discount"] isEqual:@""]) {
            //No Price Discount
        }
        cell.removeWishlistClick = ^{
            NSString *url_string = [NSString stringWithFormat: @"http://api.XXX.com/api/product/wishlist_delete/%@",[_aryWishlist [indexPath.row]valueForKey:@"user_wishlist_id"]];
            [self.manager DELETE:url_string parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                [self.collectionView reloadData];
                // 这里建议把加载数据的逻辑抽成单独方法,不要直接调用viewDidLoad
                // 比如替换成 [self loadWishlistData];
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
            }];
        };
        gridcell = cell;
    }
    return gridcell;
}

另外提个小建议:你在删除wishlist的回调里直接调用[self viewDidLoad]不太规范,最好把加载数据的逻辑抽成单独的方法(比如- (void)loadWishlistData),再调用这个方法刷新数据。

这样一套操作下来,不管是用户取消触摸,还是返回页面,cell的高亮状态都会被正确重置啦。

内容的提问来源于stack exchange,提问作者Test 87

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:29:51