Как отклонить выделение UICollectionViewCell после нажатия? - PullRequest
0 голосов
/ 22 мая 2018

Я применил этот код в моем ViewController A. После нажатия UICollectionViewCell, он переместится в ViewController B. Как я могу отклонить подсвеченную ячейку, если вернуться к 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

ОБНОВЛЕНО: -

Ниже приведены коды (didselectItemAtIndexPath и cellForItemAtIndexPath) для справки: -

- (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;

        }

Ответы [ 2 ]

0 голосов
/ 22 мая 2018

Проблема в том, что вы устанавливаете цвет фона для ячейки, независимо от того, выбран он или нет.Если вы хотите использовать пользовательский цвет фона при выделении ячейки, у вас есть два варианта:

1 - Использовать пользовательский вид для ячейки с нужным цветом фона:

Проверка UITableView Ячейка выбрана Цвет?

2 - Переопределить установщик для выбранного свойства в вашей ячейке.

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

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

И удалить:

cell.contentView.backgroundColor = ThemeLightGrayColor;
0 голосов
/ 22 мая 2018

Свифт 4,1

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
    //perform segue
}
...