简体   繁体   English

将UIButton添加到UICollectionViewCell时的奇怪行为

[英]Strange behavior when adding UIButton to UICollectionViewCell

I'm trying to add a UIButton to a UICollectionViewCell . 我正在尝试将UIButton添加到UICollectionViewCell The main purpose of the button is to let users to select their favourites. 该按钮的主要目的是让用户选择自己的收藏夹。

When they click on the UIButton , the UIButton will display a glowing star image, showing that the item has been picked as favourite. 当他们单击UIButtonUIButton将显示发光的星形图像,表明该项目已被选为收藏。

Click on the UIButton again, and the button return to display the original image with an empty star, meaning deselection. 再次单击UIButton ,按钮返回以显示带有空星的原始图像,这意味着取消选择。

The way I realize this is as below: 我意识到的方式如下:

1 - In the storyboard, put an UIButton into the UICollectionViewCell , ctrl-drag the button to the cell's .h file to setup an action called 1-在情节UICollectionViewCell ,将UIButton放入UICollectionViewCellUICollectionViewCell Ctrl并将其拖到单元格的.h文件中,以设置名为

-(IBAction)favoriteBtnClick:(id)sender;

2 - In the cell's .m file, edit the action as follows: 2-在单元格的.m文件中,按如下所示编辑操作:

-(IBAction)favoriteBtnClick:(id)sender {
    if ([self.favoriteChecked isEqualToString:@"NO"]) {
        UIImage *image = [UIImage imageNamed:@"FavoriteSelected.PNG"];
        [sender setBackgroundImage:image forState:UIControlStateNormal];
        self.favoriteChecked = @"YES";
    } else if ([self.favoriteChecked isEqualToString:@"YES"]) {
        UIImage *image = [UIImage imageNamed:@"FavoriteBlank.PNG"];
        [sender setBackgroundImage:image forState:UIControlStateNormal];
    }    
}

However, when I try to run the UICollectionView , when I clicked the button on one cell, "strange" behaviors occurred: 但是,当我尝试运行UICollectionView ,当我单击一个单元格上的按钮时,发生了“奇怪”的行为:

1 - Click on one cell to turn one button to favorite glowing star, buttons on some other cells below also turn selected, after your scroll down the list. 1-向下滚动列表后,单击一个单元格可以将一个按钮转到最喜欢的发光星,下面一些其他单元格上的按钮也将变为选中状态。

2 - The switching only functions for one cycle. 2-切换仅作用一个周期。 After you finish the first round of select and deselect, there is no longer reactions upon further clicking. 完成第一轮选择和取消选择后,再单击一次将不再有反应。

Can anybody help me understand the reasons for this and probably give me some hints on how to solve it? 有人可以帮助我了解造成这种情况的原因,并可能给我一些解决方法的提示吗?

Thank you! 谢谢!

Regards, Paul 问候保罗

Add this code in your button action 在按钮操作中添加此代码

CGPoint ButtonPoint = [sender convertPoint:CGPointZero toView:self.collectionView];
    NSIndexPath *ButtonIndex = [self.collectionView indexPathForItemAtPoint:ButtonPoint];
buttonindexpath = ButtonIndex.row
[collectionView reloadData];

Now in your cellForItemAtIndexPath, check if indexPath.row == buttonindexpath, if yes give the code you gave in button action.... ie.... 现在在您的cellForItemAtIndexPath中,检查indexPath.row == buttonindexpath,如果是,则提供您在按钮操作中给出的代码。

if (indexPath.row == buttonindexpath)
{
    if ([self.favoriteChecked isEqualToString:@"NO"]) 
    {
        UIImage *image = [UIImage imageNamed:@"FavoriteSelected.PNG"];
        [sender setBackgroundImage:image forState:UIControlStateNormal];
        self.favoriteChecked = @"YES";
    }
    else if ([self.favoriteChecked isEqualToString:@"YES"]) 
    {
        UIImage *image = [UIImage imageNamed:@"FavoriteBlank.PNG"];
        [sender setBackgroundImage:image forState:UIControlStateNormal];
    }

} }

Hope this works fine 希望一切正常

This is happening because of cell reuse. 这是由于单元重用而发生的。 since self.favoriteChecked is a property inside the custom collection cell class, and its value is updated only inside that class causes this issue. 因为self.favoriteChecked是自定义集合单元类中的一个属性,并且其值仅在该类中被更新才导致此问题。

Solution to this problem is, if you are using custom object model to populate collection cells from viewcontroller, then on favourite button click update the favourite status on that custom object and in cellForItemAtIndexPath method check the value of favourite status and update the favoriteChecked status of custom collectionviewcell class. 解决此问题的方法是,如果使用自定义对象模型从Viewcontroller填充集合单元,则在“收藏夹”按钮上单击以更新该自定义对象的收藏夹状态,并在cellForItemAtIndexPath方法中检查收藏夹状态的值并更新自定义的favoriteChecked状态collectionviewcell类。

If you are not using custom object, you need to keep a list of favourite items separately either indexpaths of favourite item or unique identifier of fav item. 如果不使用自定义对象,则需要单独保存收藏夹项目列表,或者收藏夹项目的索引路径或收藏夹项目的唯一标识符。 Then in cellForItemAtIndexPath method check the value of favourite status and update the favoriteChecked status of custom collectionviewcell class. 然后在cellForItemAtIndexPath方法中检查收藏夹状态的值并更新自定义collectionviewcell类的favoriteChecked状态。

This is no no a strange behaviour. 这不是一个奇怪的行为。 You have load the cells from the model data objects. 您已从模型数据对象加载单元。 You have to track & keep the state change of button clicked data in an array & then load the cell data values. 您必须跟踪并保持数组中单击按钮的数据的状态更改,然后加载单元格数据值。

Here is a sample code : which aligns with solution to your problem. 这是一个示例代码:与您的问题的解决方案保持一致。

NSMutableArray *favoriteStatusArray = [[NSMutableArray alloc]init];
// assume favoriteStatusArray is initialized with all BOOL as NO.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    SomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.favoriteButton.tag = indexPath.row;
    [cell.favoriteButton addTarget:self action:@selector(peformFavoriteChange:) forControlEvents:UIControlEventTouchUpInside];

    if([[favoriteStatusArray objectAtIndex:indexPath.row] boolValue] == NO)
        UIImage *image = [UIImage imageNamed:@"FavoriteSelected.PNG"];
        [cell.favoriteButton setBackgroundImage:image forState:UIControlStateNormal];
    }else {    
    UIImage *image = [UIImage imageNamed:@"FavoriteBlank.PNG"];
    [cell.favoriteButton setBackgroundImage:image forState:UIControlStateNormal];    
   }
    return cell;
}


-(void)peformFavoriteChange:(UIButton *)sender
{
    BOOL oldValue = [[favoriteStatusArray objectAtIndex:sender.tag] boolValue];
    [favoriteStatusArray replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:!oldValue]];

    [myCollectionView reloadData]; 
}

Hope you got the idea of doing this. 希望您有这样做的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 奇怪的UILabel和UIButton行为 - Strange UILabel and UIButton behavior UICollectionViewCell中的不透明UILabel具有奇怪的呈现行为 - Opaque UILabel in a UICollectionViewCell has strange rendering behavior 将彩色UIImageView添加到UITableViewCell时的奇怪行为 - Strange behavior when adding colorful UIImageView to a UITableViewCell 在UICollectionViewCell中使用时,UIButton交互不平滑 - UIButton interaction is not smooth when used in UICollectionViewCell 在UITableView的滚动电影胶片的每个UICollectionViewCell中添加UIButton - Adding UIButton in each UICollectionViewCell in Scrolling Filmstrip within UITableView 添加子视图时,iOS出现奇怪的键盘隐藏行为 - iOS strange keyboard-hiding behavior when adding a subview 添加到自定义 UICollectionViewCell 时出现意外的 CAShapeLayer 行为 - Unexpected CAShapeLayer behavior when added to custom UICollectionViewCell 当button是UICollectionViewCell的子视图时,不会调用UIButton的目标方法 - Target method for UIButton not called when button is subview of UICollectionViewCell UICollectionViewCell到UIButton专注于tvOS - UICollectionViewCell to UIButton Focus in tvOS 将UIButton设置为UICollectionViewCell的大小 - Set a UIButton to the size of a UICollectionViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM