在Scrolling Filmstrip中的每个UICollectionViewCell中调用UIButton

这是对我以前的一个后续问题,这次我在每个UICollectionViewCell中添加了UIButton的问题。 在深入研究这个问题之前,让我把目前所有的情况都介绍给大家。

以下是我的UICollectionView在UITableView中的Scrolling Filmstrip样式的工作原理的基本概要:

  • 用一个自定义的UITableViewCell创build一个普通的UITableView
  • 创build一个将被添加到单元格的contentView的自定义UIView
  • 自定义UIView将包含一个UICollectionView
  • 自定义UIView将成为UICollectionView的数据源和委托,并pipe理UICollectionView的stream布局
  • 使用自定义UICollectionViewCell来处理收集视图数据
  • 使用NSNotification通知主控制器的UITableView何时已经select一个集合视图单元并加载详细视图。

到目前为止,我已经能够在每个UICollectionViewCell中添加UIButton,并且当我点击UIButton时,它的图像将变成勾选标记,但是当我向上/向下滚动时会出现问题。 一旦带有选中标记UIButton的单元格离开屏幕并再次在屏幕上滚动,UIButton图像就开始混乱了。 例如,当单元格1中的UIButton图像应该被选中标记但不是。 相反,选中标记会出现在另一个单元格中。

下面是我的相关代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PostsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PostsCollectionViewCell" forIndexPath:indexPath]; NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]]; cell.postTextLabel.text = [cellData objectForKey:@"text"]; cell.locationNameLabel.text = [cellData objectForKey:@"locationName"]; // >>> Select Button <<< UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom]; [selectButton setFrame:CGRectMake(110, 150, 20, 20)]; [selectButton setImage:[UIImage imageNamed:@"uncheckedDot.png"] forState:UIControlStateNormal]; [selectButton setImage:[UIImage imageNamed:@"checkedDot.png"] forState:UIControlStateSelected]; [cell.contentView addSubview:selectButton]; [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; // >>> End Select Button <<<< return cell; } // >>> Select Button Method <<< -(void) buttonPressed:(UIButton *)sender { if([sender isSelected]){ //... [sender setSelected:NO]; } else { //... [sender setSelected:YES]; } } 

任何帮助将不胜感激! 提前致谢。

在这里输入图像说明

人,你应该知道单元格(UITableView和UICollectionView)都是通过UIKit背后的重用deque重用的,并且没有义务保持它们在表视图或集合视图中的状态一致。 你需要做的是确保dequeueReusableCellWithReuseIdentifier:的返回值然后根据你的数据模型的状态被正确地初始化(或者在你的情况下,重新初始化)。 基本上,你需要在数组或任何其他数据结构中存储你的“checkbox”(或“checkbox”,你有什么)的状态。 然后,当你调用dequeueReusableCellWithReuseIdentifier: ,你应该把这个存储状态应用到你刚刚得到的返回值(你要返回的集合视图的单元格)。 希望它解释得够好。 祝你好运。

在数据源中维护哪个单元格是通过为每个数据源字典添加一个键来select的。 例如:@“isSelected”

然后在cellForRowAtIndexPath中:

 if ([[cellData valueForKey:@"isSelected"] boolValue]) { [selectButton setSelected:NO]; } else { [selectButton setSelected:YES]; } 

并在你的-(void) buttonPressed:(UIButton *)sender方法:

 CGPoint point = [self.collectionView convertPoint:CGPointZero fromView:sender]; NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point]; NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]]; if ([[cellData valueForKey:@"isSelected"] boolValue]) { [cellData setValue:@"false" forKey:@"isSelected"]; } else { [cellData setValue:@"true" forKey:@"isSelected"]; } [self.framesCollectionView reloadItemsAtIndexPaths:@[indexPath]]; 

在你的cellForRowAtIndexPath中为UIButton设置一个标签。 然后在将UIButton作为子视图添加到单元格的contentView之前,检查是否存在具有此标记的视图。 如果单元格被重新使用,只需使用已经存在的button。