我有一个UICollectionView,它包含10个项目(从Parse.com检索到的图像)每个单元格覆盖整个屏幕,并设置为水平滚动。
在单元格里面,我有一个像按钮一样的按钮'点击它时会改变颜色。问题是,如果我单击单元格1中的like按钮,则单元格3,单元格5等也会更改按钮颜色..如果单击单元格0,则单元格2,单元格4等也会更改它的颜色
我尝试更改UICollectionView单元格大小,并使得10个单元格适合屏幕。当我这样做时,没有按钮重复。唯一的变化是我点击的按钮。任何想法为什么会发生这种情况,我该如何解决?我需要将细胞作为整个屏幕。
我的代码:
VestimentaDetailViewController.m
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
VestimentaDetailCell *cell = (VestimentaDetailCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.progressView.hidden = NO;
[cell.progressView setProgress:0.02];
cell.imageFile.image = [UIImage imageNamed:@"loadingLook.png"];
PFFile *storeLooks = [self.vestimenta objectForKey:[NSString stringWithFormat:@"image_%ld", (long)indexPath.item]];
[storeLooks getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error && data.length > 0) {
cell.imageFile.image = [UIImage imageWithData:data];
} else {
cell.progressView.hidden = YES;
}
} progressBlock:^(int percentDone) {
float percent = percentDone * 0.02;
[cell.progressView setProgress:percent];
if (percentDone == 100){
cell.progressView.hidden = YES;
} else {
cell.progressView.hidden = NO;
}
}];
return cell;
}
VestimentaDetailCell.m
- (IBAction)likeLook:(id)sender {
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:@"Like.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:@"Liked.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
NSLog(@"Liked Image");
}
}
答案 0 :(得分:0)
集合和表格视图可回收其单元格。我认为正在发生的事情是,如果你喜欢细胞1,那么细胞3,细胞5,依此类推......都在使用再生细胞1.
要解决此问题,请将正在出列的所有单元格的按钮颜色设置为cellForRowAtItemPath作为默认按钮颜色。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// get cell and stuff
...
cell.likeButton.backgroundColor = [UIColor whiteColor];
}