【问题标题】:How to dismiss highlight UICollectionViewCell after clicked?单击后如何关闭突出显示的 UICollectionViewCell?
【发布时间】:2018-05-22 12:09:32
【问题描述】:

我在 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];
}

更新:-

请找到以下代码(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;

        }

【问题讨论】:

  • 重新加载collectionview in viewwillappear
  • 嗨 RajeshKumar,试过了,但没有运气,有什么想法吗?
  • 单击单元格时,您推送到 Viewcontroller B 那么为什么要设置单元格的背景颜色?
  • 嗨 Nirav,我的预期结果是在用户按下时突出显示单元格

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

斯威夫特 4.1

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

【讨论】:

    【解决方案2】:

    问题是您正在为单元格设置背景颜色,无论它是否被选中。如果您想在选择单元格时使用自定义背景颜色,您有两种选择:

    1 - 为具有所需背景颜色的单元格使用自定义视图:

    查看UITableView Cell selected Color?

    2 - 覆盖单元格中选定属性的设置器。

    - (void)setSelected:(BOOL)selected {
        [super setSelected:selected];
    
        if (selected) {
            self.contentView.backgroundColor = ThemeLightGrayColor;
        } else {
            self.contentView.backgroundColor = UIColor.clearColor();
        }
    }
    

    然后删除:

    cell.contentView.backgroundColor = ThemeLightGrayColor;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-24
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多