UICollectionViewReusableView 错误:选择一个标题有时会触发另一个

Posted

技术标签:

【中文标题】UICollectionViewReusableView 错误:选择一个标题有时会触发另一个【英文标题】:UICollectionViewReusableView Error: Selecting one header sometimes triggers the other 【发布时间】:2014-07-04 08:12:00 【问题描述】:

UICollectionViewReusableView 错误:选择一个标头有时会触发另一个标头

UICollectionView 有 2 个部分。每个都有一个带有按钮的标题,该按钮可以更改textDeleteActiveimageDeleteActiveBOOL 状态,然后重新加载一个部分。这些用于显示每个单元格上的删除按钮是否隐藏。但是,如果我交替并在触摸一个按钮后触摸另一个按钮,那么它似乎将它们链接起来。之后触摸第二个按钮会触发两个 BOOL 变量进行更改。仅当再次触摸第一个按钮以以某种方式取消链接时,才能解决此问题。我无法弄清楚为什么或如何存在链接。

代码(删除了大部分不相关的代码):

UICollectionReusableView 标题视图和UICollectionViewCell 单元格都有@property (nonatomic, strong) IBOutlet deleteButton。当然,它们每个都有一个实际的 UIButton。

myUICollectionViewController:

@property (nonatomic) BOOL textDeleteActive;
@property (nonatomic) BOOL imageDeleteActive;

- (void)TextHeaderDeleteButtonDynamicHandler

    NSLog(@"texthead");
    self.textDeleteActive = !self.textDeleteActive;
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];

- (void)ImageHeaderDeleteButtonDynamicHandler

    NSLog(@"imagehead");
    self.imageDeleteActive = !self.imageDeleteActive;
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];


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

    long fRow = [indexPath row];
    switch ([indexPath section]) 
        case 0:
            if (true) 

                HCSShortCutTextViewCell *theCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyShortCut" forIndexPath:indexPath];


                if (self.imageDeleteActive) 
                    theCell.deleteButton.hidden = NO;
                 else 
                    theCell.deleteButton.hidden = YES;
                


                [theCell.deleteButton addTarget:self action:@selector(ImageCellDeleteButtonDynamicHandler:event:) forControlEvents:UIControlEventTouchUpInside];
                return theCell;
            
            break;
        case 1:
            if (true) 

                HCSCustomViewCell *theCustCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCustom" forIndexPath:indexPath];

                //no image

                if (self.textDeleteActive) 
                    theCustCell.deleteButton.hidden = NO;
                 else 
                    theCustCell.deleteButton.hidden = YES;
                


                [theCustCell.deleteButton addTarget:self action:@selector(TextCellDeleteButtonDynamicHandler:event:) forControlEvents:UIControlEventTouchUpInside];
                return theCustCell;
                
            break;
        default:
            return nil;
            break;
    

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    //handled by the storyboard segue


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

    if (kind == UICollectionElementKindSectionHeader) 
        switch ([indexPath section]) 
            case 0:
                if (true) 
                    HCSMyHeaderReusableView *theCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
                    theCell.titleLabel.text = @"Image Shortcuts";
                    NSLog(@"ima");
                    [theCell.deleteButton addTarget:self action:@selector(ImageHeaderDeleteButtonDynamicHandler) forControlEvents:UIControlEventTouchUpInside];
                    return theCell;
                
                break;
            case 1:
                if (true) 
                    HCSMyHeaderReusableView *theCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
                    theCell.titleLabel.text = @"Text Shortcuts";
                    NSLog(@"tex");
                    [theCell.deleteButton addTarget:self action:@selector(TextHeaderDeleteButtonDynamicHandler) forControlEvents:UIControlEventTouchUpInside];
                    return theCell;
                
                break;
            default:
                return nil;
                break;
        
     else
        return nil;




- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //set defaults
    self.textDeleteActive = NO;
    self.imageDeleteActive = NO;


- (void)TextCellDeleteButtonDynamicHandler:(id)sender event:(id)event

    NSLog(@"textcell");
    [self deleteItemAndReloadCollectionView:sender event:event defaultsKey:@"textShortcuts"];

- (void)ImageCellDeleteButtonDynamicHandler:(id)sender event:(id)event

    NSLog(@"imagecell");
    [self deleteItemAndReloadCollectionView:sender event:event defaultsKey:@"shortcuts"];


【问题讨论】:

【参考方案1】:

用 hack 修复了它。制作了两个删除按钮,每个按钮只出现一个,因此addTarget: 用于不同的按钮,因此在一个按钮上没有使用多个目标选择器。显然,目标是通过重复使用出队来实现的。

【讨论】:

以上是关于UICollectionViewReusableView 错误:选择一个标题有时会触发另一个的主要内容,如果未能解决你的问题,请参考以下文章