【发布时间】: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