0

UICollectionViewReusableView Erreur: Sélection d'un en-tête déclenche parfois l'autre têteUICollectionViewReusableView Erreur: Sélection d'un en-tête déclenche parfois l'autre

UICollectionView a 2 sections. Chacun a un en-tête avec un bouton qui change l'état de textDeleteActive ou imageDeleteActive, puis recharge une section. Ceux-ci sont utilisés pour montrer si le bouton Supprimer sur chaque cellule est caché ou non. Mais si j'alterne et, après avoir appuyé sur un bouton, toucher l'autre, alors il semble les relier. Ensuite, toucher le deuxième bouton déclenche les deux variables BOOL pour changer. Ceci n'est résolu que si le premier bouton est à nouveau touché pour le dissocier d'une manière ou d'une autre. Je ne peux pas comprendre pourquoi ou comment il y a un lien.

code (avec la plupart des codes sans lien supprimé):

Les deux vues d'en-tête UICollectionReusableView et les deux cellules ont UICollectionViewCell@property (nonatomic, strong) IBOutlet deleteButton. Ils ont bien sûr chacun un UIButton réel.

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

} 

Répondre

0

fixe avec un hack. Fait deux boutons de suppression et avait seulement un apparaître par chacun de sorte que addTarget: a été utilisé sur différents boutons, donc pas de sélecteurs de cibles multiples étant utilisés sur un seul bouton. Apparemment, la cible passe par la réutilisation de la file d'attente.

Questions connexes