0

I applied these code in my ViewController A. After clicked UICollectionViewCell, it will push to ViewController B. How can I dismiss highlighted cell if back to ViewController A?

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = ThemeLightGrayColor;
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

enter image description here

UPDATED:-

Please find below codes (didselectItemAtIndexPath and cellForItemAtIndexPath) for your reference:-

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) 
    {
        _productID = [_aryWishlist[indexPath.row]valueForKey:@"product_id"];

       dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *url_string2 = [NSString stringWithFormat: @"http://api.XXX.com/api/product/product_details/%@",_productID];
            NSData *data2 = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string2]];
            productDetailsAry = [NSJSONSerialization JSONObjectWithData:data2 options:kNilOptions error:&error];

        });

        Product_ViewController *prodVC = [[Product_ViewController alloc] init];

        [self.navigationController pushViewController:prodVC animated:YES];
    }
    else
    {
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *gridcell = nil;
        if(_aryWishlist.count > 0)
        {
            WishlistCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WishlistCellID forIndexPath:indexPath];

                [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];
                        [self viewDidLoad];
                    }
                                 failure:^(NSURLSessionDataTask *task, NSError *error) {

                                 }];

                };
            }
            gridcell = cell;

        }
Gaurav Patel
  • 532
  • 1
  • 5
  • 19
Test 87
  • 101
  • 2
  • 10

2 Answers2

0

Swift 4.1

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
    //perform segue
}
Harry McGovern
  • 517
  • 5
  • 19
0

The problem is that you are setting a background color for the cell, no matter if it is selected or not. If you want to use a custom background color when the cell is selected you have two options:

1 - Use a custom view for the cell with the background color you want:

Check UITableView Cell selected Color?

2 - Override the setter for the selected property in your cell.

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];

    if (selected) {
        self.contentView.backgroundColor = ThemeLightGrayColor;
    } else {
        self.contentView.backgroundColor = UIColor.clearColor();
    }
}

And remove:

cell.contentView.backgroundColor = ThemeLightGrayColor;
crom87
  • 1,141
  • 9
  • 18