Voici ma vieille question LinkProgramme Crash lorsque je clique sur un commutateur?
je pose une question, mais je mets la réponse dans mon code Il peut compilateur sans erreur
Mais quand je clique sur le bouton, le plantage du programme
I je veux juste cliquer sur le commutateur, que de retourner quelle ligne je clique
Voici mon code
d'abord créer un LightTableViewController.h/.m
Que je crée LightCell0.h/.m
dans LightCell0.h
@interface LightCell0 : UITableViewCell {
UILabel *lightLocation;
UIImageView *lightImageView;
UISwitch *lightSwitch;
}
@property(nonatomic,retain) UILabel *lightLocation;
@property(nonatomic,retain) UIImageView *lightImageView;
@property(nonatomic,retain) UISwitch *lightSwitch;
- (void)switchlightswitch:(id)sender;
J'ai besoin chaque cellule sera une image, Textlabel, interrupteur à l'intérieur
Dans LightCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
lightLocation = [[UILabel alloc]init];
lightLocation.textAlignment = UITextAlignmentLeft;
lightLocation.font = [UIFont boldSystemFontOfSize:20];
lightLocation.backgroundColor = [UIColor blackColor];
lightLocation.textColor =[UIColor whiteColor];
lightImageView = [[UIImageView alloc]init];
lightSwitch = [[UISwitch alloc]init];
[self.contentView addSubview:lightLocation];
[self.contentView addSubview:lightImageView];
[self.contentView addSubview:lightSwitch];
[lightSwitch addTarget:self action:@selector(switchlightswitch:) forControlEvents:UIControlEventValueChanged];
// Initialization code
}
return self;
}
- (void) switchlightswitch: (id) expéditeur {
if (lightSwitch.on){
lightImageView.image = [UIImage imageNamed:@"lightOn.png"];
}
else {
lightImageView.image = [UIImage imageNamed:@"lightOff.png"];
}}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame = CGRectMake(boundsX+10 ,0, 44, 44);
lightImageView.frame = frame;
frame = CGRectMake(boundsX+60 ,3, 150, 44);
lightLocation.frame = frame;
frame = CGRectMake(boundsX+220, 10,0,0);
lightSwitch.frame = frame ;
}
Jusqu'à présent, le programme peut répondre quand je change l'état du commutateur, il sera également changer l'image.
mais si je mets ce code dans, je peux compilateur, que plantage si je touche un interrupteur
[lightSwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged]
;
et donner un vide
- (void)switchToggled:(id)sender {
UISwitch *theSwitch = (UISwitch *)sender;
UITableViewCell *cell = (UITableViewCell *)theSwitch.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
if(theSwitch.on) {
NSLog(@"You Switch On the NodeID:%i ",indexPath.row);
}
else {
NSLog(@"You Switch Off the NodeID:%i ",indexPath.row);
}
}
le message d'accident est "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[LightCell0 indexPathForCell:]: unrecognized selector sent to instance
"
ne quelqu'un sait ce qui se passe avec mon code?
Je ne sais pas ....... Parce que je référence juste la réponse et les copier directement dans mon code .... –
Vodkhang est correct, votre logique/conception est faux à un moment donné. La «vue d'ensemble» de la «vue d'ensemble» de l'interrupteur est votre cellule et non la vue de table comme vous l'attendez – Liam
ce n'est pas ma réponse ... juste quelqu'un me dit que cela peut être du travail ... alors je copie sa réponse. –