gpt4 book ai didi

ios - 如何维护 UICollectionViewCell 中的按钮状态?

转载 作者:行者123 更新时间:2023-11-28 22:08:04 25 4
gpt4 key购买 nike

我正在使用 UICollectionView 并为其单元格设置了一个类。在单元格中,我有一个图像和一个点赞按钮,当点击它时它会改变状态(改变按钮图像)。注意:我的 collectionView 水平显示并覆盖整个屏幕。

我的问题: 单元格正在重复使用,类似的按钮状态每两个单元格更改一次。如果我喜欢 image1,然后是 image3、image5 并更改“赞”按钮。如果我喜欢 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");

}
}

最佳答案

通过将按钮的状态链接到您的数据源或将索引映射到选定状态的数据结构(可能是 NSMutableDictionary 或 NSMutableArray)来跟踪按钮的状态。

下面只是一个建议,这个问题有几种解决方法,主要看你开发的风格。

例子一:
向数据源中的任何对象添加一个状态字段(可能会使您的对象类变得困惑)

示例 2:如果使用字典,键是单元格索引,值可以为空。如果选择了按钮,则将其放入字典中,如果没有,则不要。这样查找速度很快,您可以很快知道它是否被选中。

-(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]];

//============================================ Modified
BOOL selected = [self isCellPathSelected:indexPath.row];
// if selected, show in the UI, otherwise revert to not selected
//============================================ Modified


[storeLooks getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error && data.length > 0) {

cell.imageFile.image = [UIImage imageWithData:data];

} else {

NSLog(@"No image found");

}

return cell;
}

//============================================ Modified
- (BOOL) isCellPathSelected:(int)index{
//look in the dictionary, if there, return yes, otherwise return no
}

关于ios - 如何维护 UICollectionViewCell 中的按钮状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23503134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com