【发布时间】:2014-05-06 19:18:21
【问题描述】:
我正在使用 UICollectionView 并为其单元格设置了一个类。 在单元格中,我有一个图像和一个 Like 按钮,当点击它时它会改变状态(更改按钮图像)。注意:我的collectionView是水平显示的,覆盖了整个屏幕。
我的问题: 单元格正在重复使用,并且每两个单元格都更改类似按钮状态。如果我喜欢 image1,那么 image3、image5 以及更改 Like 按钮。如果我喜欢 image2,那么 image4、image6 等会改变它们的状态。
我知道这与细胞的重复使用有关。我的问题是如何修复它,以便每个按钮都是唯一的,或者按钮的状态在每个单元格中设置为未选中。 我发现了涉及 UITableView 的类似问题,但答案似乎对我没有帮助。所以希望我能在这里找到答案。
我的代码: 注意:这是一个 DetailView,因此数据已作为 PFObject 传递(我使用 Parse.com 后端)
-(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.imageFile.image = [UIImage imageNamed:@"LoadLook.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 {
NSLog(@"No image found");
}
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];
UIImageView *like = [[UIImageView alloc] initWithFrame:CGRectMake(120, 220, 100, 100)];
like.image = [UIImage imageNamed:@"Love.png"];
[self addSubview:like];
[UIView animateWithDuration:2 animations:^{like.alpha = 0.0;}];
NSLog(@"Liked Image");
}
}
【问题讨论】:
标签: ios parse-platform uicollectionviewcell