我正在使用一个UICollectionViewCell子类,它有两个按钮“add to cart”和“Remove”。我已经编程显示已经添加到购物车的那个单元格上的删除,但是collectionView只更改了最后添加的产品的标题。我认为重复使用该单元存在一些问题。
这是我的cellForItemAtIndexPath
- (ShoppingCellCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ShoppingCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShoppingViewCell" forIndexPath:indexPath];
NSString *imgURLSTR = [NSString stringWithFormat:@"%@%@",IMAGE_BASEURL,[[[data valueForKey:@"products"] valueForKey:@"imagepath"] objectAtIndex:indexPath.row]];
imgURLSTR = [imgURLSTR stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
[cell.itemImageView sd_setImageWithURL:[NSURL URLWithString:imgURLSTR] placeholderImage:[UIImage imageNamed:@"telebuy_logo.png"]];
[cell.itemTitle setText:[NSString stringWithFormat:@"%@",[[[data valueForKey:@"products"] valueForKey:@"desc"] objectAtIndex:indexPath.row]]];
[cell.rateLabel setText:[NSString stringWithFormat:@"INR %@",[[[data valueForKey:@"products"] valueForKey:@"salevalue"] objectAtIndex:indexPath.row]]];
[cell.featureButtonOutlet addTarget:self action:@selector(featuresBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.featureButtonOutlet setTag:indexPath.row];
NSDictionary*dict = [[data valueForKey:@"products"]objectAtIndex:indexPath.item];
NSString* singleProductFromAllProducts = [dict valueForKey:@"id"];
for (int i = 0 ; i<dataC.count; i++) {
if ([singleProductFromAllProducts isEqualToString:[[dataC objectAtIndex:i]valueForKey:@"prodid"]]) {
NSLog(@"Item is %@",[[dataC objectAtIndex:i]valueForKey:@"prodid"]);
NSLog(@"%@",singleProductFromAllProducts);
[cell.addToCartButtonOutlet setTitle:@"Remove" forState:UIControlStateNormal];
cell.addToCartButtonOutlet.backgroundColor = [UIColor grayColor];
}
else
{
[cell.addToCartButtonOutlet setTitle:@"Add To Cart" forState:UIControlStateNormal];
cell.addToCartButtonOutlet.backgroundColor = [UIColor redColor];
}
}
[cell.addToCartButtonOutlet addTarget:self action:@selector(addToCartBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.addToCartButtonOutlet setTag:indexPath.row];
return cell;
}
其中addToCartButtonOutlet是按钮插座,dataC是添加到购物车的产品数组,“singleProductFromAllProducts”是indexPath的产品。我将每个产品与dataC的产品进行比较,如果产品在dataC中可用,然后更改按钮插座以进行删除,但它仅显示仅在一个产品上删除。
这是我的自定义类UICollectionViewCell
#import "ShoppingCellCollectionViewCell.h"
#import "AppDelegate.h"
#import "ShoppingViewController.h"
@implementation ShoppingCellCollectionViewCell
- (void)awakeFromNib {
// Initialization code
_itemImageView.layer.borderWidth = 0.5;
_itemImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
[_addToCartButtonOutlet setBackgroundColor:[APPDELEGATE colorFromHexString:@"#035098"]];
[_addToCartButtonOutlet setBackgroundColor:[APPDELEGATE colorFromHexString:@"#ee1c25"]];
}
需要帮助。