2012-09-05 3 views
1

Je bouton avoir du mal à ajouter à mon UITableViewCell, la cellule a deux UILabel s et deux UIImageView s, parfois le UIImageView contiendra une image et parfois un bouton:mon UITableViewCell bouton de réglage

enter image description here

dans mon UITableViewCell sous-classe je:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

    firstLock = [[UILabel alloc]init]; 
    [firstLock setBackgroundColor:[UIColor clearColor]]; 
    firstLock.textAlignment = UITextAlignmentLeft; 
    firstLock.font = [UIFont fontWithName:@"Arial-BoldMT" size:17]; 

    secondLock= [[UILabel alloc]init]; 
    [secondLock setBackgroundColor:[UIColor clearColor]]; 
    secondLock.textAlignment = UITextAlignmentRight; 
    secondLock.font = [UIFont fontWithName:@"Arial-BoldMT" size:17]; 

    firstLockImage = [[UIImageView alloc]init]; 

    secondLockImage = [[UIImageView alloc] init]; 

    [self.contentView addSubview:firstLock]; 
    [self.contentView addSubview:secondLock]; 
    [self.contentView addSubview:firstLockImage]; 
    [self.contentView addSubview:secondLockImage]; 
    } 
    return self; 
} 

lorsque l'un des UIImageView s est juste une image pas de problème, mais il se bloque lorsque je ajoutez un UIButton (imagé) en tant que sous-vue.

Dans la mise en œuvre UITableViewDataSource:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 



UIImage *btnImage = [UIImage imageNamed:@"bike_ok.png"]; 

UIButton *button =[UIButton alloc]; 
[button setImage:btnImage forState:UIControlStateNormal]; 

[cell.secondLockImage addSubview:button]; 

Ajout d'un bouton comme sous-vue de la vue de l'image se bloque:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requesting the window of a view (<UIButton: 0x7bac7d0; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.' 
*** First throw call stack: 

Qu'est-ce que je manque?

Merci!

Ajoutez juste! Parce que le UIImageView a NON par défaut et le bouton ne fonctionne pas sans cela!

+0

Am J'ai raté quelque chose? Je ne vois pas où vous créez (et réutilisez) la cellule elle-même dans votre code. L'action généralement effectuée dans tableView: cellForRow: – Leonardo

+0

Je ne fais que copier le code correspondant – user1256477

Répondre

5

Si vous lisez l'erreur de l'accident, il est assez facile de voir où votre problème est. Vous n'avez pas initialisé votre bouton.

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,0,0)]; 

ou vous pouvez utiliser initWithCoder.

Hope this helps

Sam

1

Lire le texte d'exception - il est dit:

Ce point de vue n'a probablement pas reçu initWithFrame: ou initWithCoder:

Juste quelques lignes dans votre question, vous envoyez des messages à une instance UIButton que vous avez seulement alloc 'd et n'a envoyé aucun message init... à. C'est votre erreur.

En outre, vous ne devez pas appeler directement le alloc/ init paire sur UIButton car il est un cluster de classe et vous devez généralement utiliser +[UIButton buttonWithType:] pour obtenir une instance de bouton.

EDIT Je ne suis pas sûr à 100% à ce sujet, en fait. Mais vous ne savez pas exactement ce que vous obtiendrez si vous faites initWithFrame: alors je continuerais avec buttonWithType: pour obtenir un bouton personnalisé, ce qui est ce que vous voulez. FIN EDIT

Donc, changer cette ligne à:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

Hope this helps!

+0

Très bonne explication! J'ai essayé et si j'init le bouton comme vous l'avez dit, je dois ajouter button.frame pour définir le cadre. – user1256477

+1

Merci! Et oui, vous devez définir le cadre. Vous pouvez également faire 'sizeToFit' après avoir défini l'image et cela vous donnera probablement un bon point de départ. –

1

changement UIButton *button =[UIButton alloc]; (alloc w/o initialisation?) À

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; 
//set frame 
[button setFrame:(CGRect){x,y,w,h}]; 

+buttonWithType gère les alloc/init pour votre objet UIButton