Вот мой старый вопрос Ссылка
Я задаю вопрос раньше, но я поставил ответ в своем коде Может компилятор без ошибок
Но когда я нажимаюпереключатель , сбой программы
Я просто хочу нажать на переключатель, а затем вернуть строку, которую я нажимаю
Вот мой код
Сначала я создаю LightTableViewController.h/.m
Чем я создаю LightCell0.h/.m
в 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;
Мне нужно, чтобы в каждой ячейке было изображение, текстовая метка, переключатель внутри
В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) переключатель: (id) отправитель {
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 ;
}
Пока что программа может ответитькогда я изменяю статус переключателя, он также меняет изображение.
, но если я вставлю этот код, я могу скомпилировать, чем произойдет сбой, если я коснусь любого переключателя
[lightSwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged]
;
и дать void
- (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);
}
}
сообщение об ошибке "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[LightCell0 indexPathForCell:]: unrecognized selector sent to instance
"
Кто-нибудь знает, что происходит с моим кодом?