2013-07-09 6 views
0

Je souhaite créer une cellule de tableau personnalisée qui gère automatiquement le remplissage des champs de texte. Mon idée est que je vais juste passer un objet à la classe de cellule et la cellule remplira alors automatiquement les champs. Tout fonctionne bien, sauf qu'aucun bouton à l'intérieur de la cellule fonctionne, ils sont tous non cliquables. Qu'est-ce que je fais mal?Personnalisé Réutilisable UITableViewCell

écran principal:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSInteger row = [indexPath row]; 
     Claim *claim = [statementsArray objectAtIndex:row]; 
     NSString * strIndentifier; 

     strIndentifier = @"StatementDetailsCellIdentifier"; 

     StatementDetailsCell *cell = (StatementDetailsCell *) [tableView dequeueReusableCellWithIdentifier:strIndentifier]; 


     cell.hasWarranty = claim.hasWarranty; 
     if(cell == nil) 
     { 
      [[NSBundle mainBundle] loadNibNamed:@"StatementDetailsCell" owner:self options:nil]; 
      cell = [statementCell initWithClaim:claim reuseIdentifier:strIndentifier]; 

      cell.selectionStyle = UITableViewCellSelectionStyleNone;   
     } 

      return cell; 
    } 

Cell.m:

-(id)initWithClaim:(Claim *)_claim reuseIdentifier:(NSString *)reuseIdentifier 
    { 
     self = [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; 
     claim = _claim; 

     [self populate]; 
     return self; 
    } 

    -(void)populate 
    { 
     barcodeLabel.text = claim.barcode; 

     NSLog(@"claim is %@", [claim description]); 

     if(claim.points == 0 || claim.points == 0.00) 
      valueLabel.text = @"Pending"; 
     else 
      valueLabel.text = [NSString stringWithFormat:@"£%.2f", claim.points]; 

     modelLabel.text = claim.product; 
     warrantyLabel.text = claim.warranty.name; 
     APIRequest *apiRequest = [[APIRequest alloc] init]; 
     dateLabel.text = [apiRequest parseDate:claim.date]; 
     //hasWarranty = claim.hasWarranty; 

     double timeS = [apiRequest getUnixTimestamp:claim.date]; 
     NSDate *now = [NSDate date]; 
     NSDate *trueDate = [NSDate dateWithTimeIntervalSince1970:timeS]; 
     double timeDiffrece = [now timeIntervalSinceDate:trueDate]; 
     double threemonths = 90*24*3600; 

     //Warranty Button 

     if(claim.hasWarranty) 
     { 
      UIImage *buttonImage; 
      //13-6-2013 
      if([claim.warranty.name isEqualToString:@"Pending"]) { 
       buttonImage = [UIImage imageNamed:@"imgBtnWarrantyPending.png"]; 
       pendingHelp.hidden = NO; 
      } else { 
       pendingHelp.hidden = YES; 
       buttonImage = [UIImage imageNamed:@"warranty_claimed.png"]; 
      } 
      [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
      [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted]; 
      [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 
     } 
     else 
     { 
      pendingHelp.hidden = YES; 
      if(false) { 
       [warrantyButton setBackgroundImage:nil forState:UIControlStateNormal]; 
       [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 
      } else { 
       [warrantyButton setTag:claim.ID]; 
       UIImage *buttonImage = [UIImage imageNamed:@"add_warranty.png"]; 
       [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
       [warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted]; 
       [warrantyButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchDown]; 
       warrantyButton.userInteractionEnabled = YES; 
       warrantyButton.enabled = YES; 

      } 
     } 
    } 
+0

A quoi sert-il? 'if (false) {' – Tala

+0

C'est juste un hack temporaire pour contourner certaines des logiques prévues. –

+0

Avez-vous initialisé warrantyButton, sinon tout le code ne fonctionnera pas en silence – Tala

Répondre

0

peut-être votre objectif serait de supprimer la méthode. Veuillez attribuer un point d'arrêt à la méthode de remplissage de la méthode populate et essayer d'afficher ou de masquer warrantyButton si la cible est assignée pour vérification. et vérifiez le nom des images.

if(claim.hasWarranty) 
    { 
     UIImage *buttonImage; 
     //13-6-2013 
     if([claim.warranty.name isEqualToString:@"Pending"]) { 
      buttonImage = [UIImage imageNamed:@"imgBtnWarrantyPending.png"]; 
      pendingHelp.hidden = NO; 
     } else { 
      pendingHelp.hidden = YES; 
      buttonImage = [UIImage imageNamed:@"warranty_claimed"]; 
     } 
     [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
     [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted]; 
     [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 
     [warrantyButton setHidden:YES]; 
    } 
    else 
    { 
     pendingHelp.hidden = YES; 

     [warrantyButton setTag:claim.ID]; 
     UIImage *buttonImage = [UIImage imageNamed:@"add_warranty"]; 
     [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
     [warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted]; 
     [warrantyButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchDown]; 
     warrantyButton.userInteractionEnabled = YES; 
     warrantyButton.enabled = YES; 
     [warrantyButton setHidden:NO]; 

    } 

Peut-être que cela ne va jamais dans une autre partie pour assigner la cible. J'espère que vous le comprenez mieux.

+0

J'ai essayé cela. Il va à la ligne addTarget lors de l'utilisation des points d'arrêt. Le truc bizarre est qu'il fait setBackgroundImage correctement, mais les boutons no de la cellule sont cliquables (j'ai aussi utilisé userInteractionEnabled) –

+0

Est-ce que ça vient dans la partie else mais ne va pas dans if (false) {} else {} pour ajouter cible. Donc jusqu'à ce que vous n'attribuez aucune cible, alors comment pourrait-il être cliquable. Veuillez essayer mon code mis à jour – chandan

0

Je crois que vous essayez de répliquer ce que le framework Sensible TableView fait déjà. Je vous recommande de vérifier d'abord.

Questions connexes