Ios 将UIButton添加到UICollectionViewCell时的奇怪行为
我正在尝试将Ios 将UIButton添加到UICollectionViewCell时的奇怪行为,ios,objective-c,uiimageview,uibutton,uicollectionviewcell,Ios,Objective C,Uiimageview,Uibutton,Uicollectionviewcell,我正在尝试将ui按钮添加到UICollectionViewCell中。该按钮的主要用途是让用户选择他们的收藏夹 当他们点击ui按钮时,ui按钮将显示一个发光的星形图像,显示该项目已被选为收藏夹 再次单击ui按钮,按钮返回显示带有空星的原始图像,表示取消选择 我认识到这一点的方式如下: 1-在故事板中,将UIButton放入UICollectionViewCell,按住ctrl键并将该按钮拖动到该单元格的.h文件中,以设置一个名为 -(IBAction)favoriteBtnClick:(id)s
ui按钮添加到UICollectionViewCell
中。该按钮的主要用途是让用户选择他们的收藏夹
当他们点击ui按钮
时,ui按钮
将显示一个发光的星形图像,显示该项目已被选为收藏夹
再次单击ui按钮
,按钮返回显示带有空星的原始图像,表示取消选择
我认识到这一点的方式如下:
1-在故事板中,将UIButton放入UICollectionViewCell
,按住ctrl键并将该按钮拖动到该单元格的.h文件中,以设置一个名为
-(IBAction)favoriteBtnClick:(id)sender;
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];
}
}
但是,当我尝试运行UICollectionView
时,当我单击一个单元格上的按钮时,出现了“奇怪”行为:
1-单击一个单元格可将一个按钮切换为最喜爱的发光星,在向下滚动列表后,下面其他一些单元格上的按钮也会切换为选中状态
2-开关仅在一个周期内起作用。完成第一轮“选择”和“取消选择”后,进一步单击将不再有反应
有谁能帮我理解这个问题的原因,并给我一些解决方法的提示吗
谢谢大家!
问候,,
Paul这是因为细胞的重复使用。因为self.favoriteChecked
是自定义集合单元格类内的一个属性,其值仅在该类内更新,因此会导致此问题
此问题的解决方案是,如果使用自定义对象模型从viewcontroller填充集合单元格,然后在“收藏夹”按钮上,单击更新该自定义对象上的收藏夹状态,并在cellForItemAtIndexPath
方法中检查收藏夹状态的值,并更新自定义collectionviewcell类的favoriteChecked
状态
如果不使用自定义对象,则需要在收藏夹项目的Expaths或fav项目的唯一标识符中分别保留收藏夹项目的列表。然后在cellForItemAtIndexPath
方法中检查Favorite status的值,并更新custom collectionviewcell类的favoriteChecked
状态。发生这种情况是因为单元格重复使用。因为self.favoriteChecked
是自定义集合单元格类内的一个属性,其值仅在该类内更新,因此会导致此问题
此问题的解决方案是,如果使用自定义对象模型从viewcontroller填充集合单元格,然后在“收藏夹”按钮上,单击更新该自定义对象上的收藏夹状态,并在cellForItemAtIndexPath
方法中检查收藏夹状态的值,并更新自定义collectionviewcell类的favoriteChecked
状态
如果不使用自定义对象,则需要在收藏夹项目的Expaths或fav项目的唯一标识符中分别保留收藏夹项目的列表。然后在cellForItemAtIndexPath
方法中检查FavoriteStatus的值,并更新CustomCollectionViewCell类的favoriteChecked
状态。这不是一个奇怪的行为。您已经从模型数据对象加载了单元。您必须在数组中跟踪并保持按钮单击数据的状态更改,然后加载单元格数据值
下面是一个示例代码:它与您的问题的解决方案一致
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];
}
希望你有这样做的想法。这不是一种奇怪的行为。您已经从模型数据对象加载了单元。您必须在数组中跟踪并保持按钮单击数据的状态更改,然后加载单元格数据值
下面是一个示例代码:它与您的问题的解决方案一致
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];
}
希望您有这样做的想法。在按钮操作中添加此代码
CGPoint ButtonPoint = [sender convertPoint:CGPointZero toView:self.collectionView];
NSIndexPath *ButtonIndex = [self.collectionView indexPathForItemAtPoint:ButtonPoint];
buttonindexpath = ButtonIndex.row
[collectionView reloadData];
现在,在您的cellForItemAtIndexPath中,检查indexPath.row==buttonindexpath,如果是,请给出您在button action中给出的代码。。。。
即
}
希望这可以正常工作在按钮操作中添加此代码
CGPoint ButtonPoint = [sender convertPoint:CGPointZero toView:self.collectionView];
NSIndexPath *ButtonIndex = [self.collectionView indexPathForItemAtPoint:ButtonPoint];
buttonindexpath = ButtonIndex.row
[collectionView reloadData];
现在,在您的cellForItemAtIndexPath中,检查indexPath.row==buttonindexpath,如果是,请给出您在button action中给出的代码。。。。
即
}
希望这一切顺利这是因为细胞的重复使用。您能展示一下cellForItemAtIndexPath
方法代码吗?非常感谢大家。刚刚解决了这个问题。事实上,我从这里所有的建议中得到了答案。首先,使用convertPoint-to-View方法在collectionView上标识单元格的indexPath。然后更新数据模型对象中的收藏夹状态。最后要求collectionView重新加载数据。问题解决了!很酷!这是因为细胞的重复使用。您能展示一下cellForItemAtIndexPath
方法代码吗?非常感谢大家。刚刚解决了这个问题。事实上,我从这里所有的建议中得到了答案。首先,使用convertPoint-to-View方法在collectionView上标识单元格的indexPath。然后更新数据模型对象中的收藏夹状态。最后要求collectionView重新加载数据。问题解决了!很酷!您的答案可能有帮助,但看起来您必须对其进行编辑,因为OP询问的是UICollectionView
,而答案代码是针对UITableView
。请编辑您的答案,@Balram Tiwari,我已经编辑了集合视图的代码。很抱歉将其误认为表视图。希望此代码运行良好您的答案可能会有所帮助,但看起来您必须对其进行编辑,因为OP询问的是UICollectionView
,而答案代码是针对UITableView
。请编辑您的答案,@Balram Tiwari,我已经编辑了集合视图的代码。很抱歉将其误认为表视图。希望这个代码可以正常工作