单击开关时程序崩溃?
这是我的老问题 链接
我之前问过一个问题,但是我把答案放在我的代码中它可以编译没有错误
但是当我单击开关时,程序崩溃
我只想单击开关,而不是返回我单击的
行是我的代码
首先我创建一个 LightTableViewController.h/.m
然后在 LightCell0.h
中创建 LightCell0.h/.m
@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)switchlightswitch:(id)sender{
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)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);
}
}
崩溃消息是“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** -[LightCell0 indexPathForCell:]:无法识别的选择器发送到实例
”
有人知道发生了什么吗继续我的代码?
Here is my old question Link
I ask a question before,But I put the answer in my code It can compiler without error
But when I click the switch ,the program crash
I just want to click the switch ,than return which row I click
Here is my code
First I create a LightTableViewController.h/.m
Than I create LightCell0.h/.m
in 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;
I need each cell will be an image ,Textlabel ,switch inside
In 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)sender{
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 ;
}
So far,the program can response when i change the switch status ,it will also change the image.
but if I put this code in ,i can compiler ,than crash if I touch any switch
[lightSwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged]
;
and give a 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);
}
}
the crash message is "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[LightCell0 indexPathForCell:]: unrecognized selector sent to instance
"
does anyone knows what's going on with my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从错误消息来看,我怀疑您的转换是错误的:
Are you certain that theSwitch is a UITableViewCell and the super view of the cell is the UITableView.崩溃告诉您您所尊重的 tableView 对象是 LightCell0 对象
From the error message, I suspect your cast is wrong:
Are you sure that the superview of theSwitch is a UITableViewCell and the super view of the cell is the UITableView. The crash tell you that the tableView object that you respect is a LightCell0 object
为了获取开关的状态,您正在使用“yourSwitch.on”,我认为这是不正确的。
将其更改为
[yourSwitch isOn]
并测试您的应用。我在您的代码中看到两个语句,您正在其中检查开关的状态。改变它们并尝试。
For getting the status of the switch, you are using "yourSwitch.on", which I don't think is correct.
Chang it to
[yourSwitch isOn]
and test your app.I see 2 statements in your code where you are checking the state of a switch. Change them and try.
我已经找到答案了!
theSwitch.superview实际上返回UITableViewCell内的contentView。
所以你应该改为:
I have found the answer!
theSwitch.superview actually returns the contentView inside UITableViewCell.
So you should change to: