Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UICollectionViewCell中维护按钮状态?_Ios_Parse Platform_Uicollectionviewcell - Fatal编程技术网

Ios 如何在UICollectionViewCell中维护按钮状态?

Ios 如何在UICollectionViewCell中维护按钮状态?,ios,parse-platform,uicollectionviewcell,Ios,Parse Platform,Uicollectionviewcell,我正在使用UICollectionView,并为其单元格设置了一个类。 在单元格中,我有一个图像和一个类似的按钮,当点击它时,它会改变状态(改变按钮图像)。注意:my collectionView水平显示,覆盖整个屏幕 我的问题:单元格正在重复使用,并且每两个单元格的like按钮状态都会发生变化。如果我喜欢image1,那么请选择image3、image5,并更改like按钮。如果我喜欢image2,那么image4、image6等会改变它们的状态 我知道这与细胞的再利用有关。我的问题是如何修复

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

我的问题:单元格正在重复使用,并且每两个单元格的like按钮状态都会发生变化。如果我喜欢image1,那么请选择image3、image5,并更改like按钮。如果我喜欢image2,那么image4、image6等会改变它们的状态

我知道这与细胞的再利用有关。我的问题是如何修复它,以便每个按钮都是唯一的,或者每个单元格中的按钮状态都设置为未选中。 我发现类似的问题涉及UITableView,但答案似乎对我没有帮助。希望我能在这里找到答案

我的代码: 注意:这是一个DetailView,因此数据已作为PFObject传递(Im使用Parse.com后端)

restrimentadetailcell.m

Like按钮有其作用:

- (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),跟踪按钮的状态

以下只是一个建议,这个问题有几种解决方法,主要基于您开发的风格。

示例1:
向数据源中的任何对象添加状态字段(可能会使对象类混乱)

例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
}

我假设VestmentAdeTailCell是UICollectionViewCell的一个子类

在.h文件中创建按钮的属性

在此类中,如果重写此方法:

- (void)prepareForReuse
{
      [super prepareForReuse];
      [yourbuttonproperty setImage:[UIImage imageNamed:@"Like.png"]
                          forState:UIControlStateNormal];
      [yourbuttonproperty setSelected:NO];
}
这通常用于清理代码,根据apple docs的定义,它声明:

当视图退出队列以供使用时,在 相应的出列方法将视图返回给代码。子类 可以重写此方法并使用它将属性重置为其 默认值,通常使视图准备好再次使用。你 不应使用此方法将任何新数据指定给视图。就是 数据源对象的责任

也许我在函数中实现的内容可能不符合您的要求,但我认为这是您需要解决的问题

这将重置按钮状态,而不会给出按下的按钮状态。这意味着你所有的按钮都处于正常状态


然后,我认为您还需要为您的like按钮提供一个委托方法,该方法由视图控制器实现。在这个委托方法中,您所要做的就是为按照William提到的方法点击的任何单元格填充字典或数组

谢谢你如此明确的回答!我想我快到了。由于我是新来的开发人员,我不得不再次请求您的帮助。你能帮我编这本字典吗?我不知道怎么做,你能帮我吗?
- (void)prepareForReuse
{
      [super prepareForReuse];
      [yourbuttonproperty setImage:[UIImage imageNamed:@"Like.png"]
                          forState:UIControlStateNormal];
      [yourbuttonproperty setSelected:NO];
}