首页 文章

在Scrolling Filmstrip中的每个UICollectionViewCell中弄乱UIButton

提问于
浏览
1

这是我的previous one的后续问题,这次我在每个UICollectionViewCell中添加了UIButton问题 . 在深入讨论这个问题之前,让我简要介绍一下迄今为止我所做的一切 .

以下是我在UITableView中滚动Filmstrip样式的UICollectionView如何工作的基本概述:

  • 使用自定义UITableViewCell创建普通的UITableView

  • 创建将添加到单元格的contentView的自定义UIView

  • 自定义UIView将包含UICollectionView

  • 自定义UIView将是UICollectionView的数据源和委托,并管理UICollectionView的流布局

  • 使用自定义UICollectionViewCell处理集合视图数据

  • 使用NSNotification在选择了集合视图单元格时通知主控制器的UITableView并加载详细信息视图 .

到目前为止,我已经能够在每个UICollectionViewCell中添加UIButton,当我点击UIButton时,它的图像将变为选中标记但是 the problem occurs when I scroll up/down. Once the cell with checked mark UIButton is off-screen and scrolled back on-screen again, UIButton image starts to get messed around. For example, when UIButton image in cell1 should be checked mark but it's not. Instead checked mark will be appeared in another cell.

以下是我的相关代码:

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

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

enter image description here

2 回答

  • 2

    伙计,你应该知道单元格(UITableView和UICollectionView)都是通过UIKit引擎下的重用deque重用的,没有义务保持它们在表视图或集合视图中的位置一致 . 您需要做的是确保 dequeueReusableCellWithReuseIdentifier: 的返回值然后根据数据模型的状态正确初始化(或者,在您的情况下,重新初始化) . 基本上,您需要在数组或任何其他数据结构中存储"checkmarks"(或"checkboxes",你有什么)的状态 . 然后,当你调用 dequeueReusableCellWithReuseIdentifier: 时,你应该将该存储状态应用于返回值(你所解释的单元格已经足够好了 . 祝你好运 .

  • 2

    通过为每个数据源字典添加密钥,在您的数据源中维护要选择的单元格 . 例如:@“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]];
    

    AND

    在您的cellForRowAtIndexPath中为UIButton设置标记 . 然后在将UIButton作为子视图添加到单元格的contentView之前检查是否已存在具有此标记的视图 . 如果单元格已被重用,只需使用现有按钮即可 .

相关问题