0

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

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

代码(删除了大多数不相关的代码):

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"];

}
4

1 回答 1

0

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

于 2014-07-04T09:42:32.057 回答