2010-09-15 55 views
1

这里是我的老问题Link程序崩溃,当我点击一个开关?

之前我问一个问题,但我把我的代码的答案,可以编译没有错误

但是当我点击开关,程序崩溃

我只是想按一下开关,不是返回哪一行我点击

这里是我的代码

首先我创建了一个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; 

创建我需要每个单元将是一个图像,为textLabel,内部

开关在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; 
} 

- (空)switchlightswitch:(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)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

没有人知道发生了什么事我的代码?

回答

1

从错误信息,我怀疑你投错了:

UITableViewCell *cell = (UITableViewCell *)theSwitch.superview; 
UITableView *tableView = (UITableView *)cell.superview; 

你肯定精选完美男的上海华是一个UITableViewCell和细胞的超视图是UITableView的。崩溃告诉你,你尊重的tableView对象是一个LightCell0对象

+0

我不知道.......因为我只是参考答案,并直接复制到我的代码.... – 2010-09-15 08:31:27

+0

Vodkhang是正确的,你的逻辑/设计在某些时候是错误的。交换机的'超级观点'的'超级观点'是你的手机,而不是你期待的 – Liam 2010-09-15 08:37:23

+0

这不是我的答案......只是有人告诉我这可能是工作......所以我复制他的答案。 – 2010-09-15 08:46:18

0

为了得到交换机的状态,你使用了“yourSwitch.on”,我认为这是不正确的。 将它改为[yourSwitch isOn]并测试您的应用程序。 我在代码中看到2条语句,用于检查交换机的状态。改变他们并尝试。

+0

但仍不知道要点击哪个开关? – 2010-09-15 10:09:15

+0

yourSwitch.on是正确的,它是属性 – vodkhang 2010-09-15 11:45:24

+0

为什么不给开关一个标签和根据标签创建一个新的NSIndexPath: NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0 ]。 – CW0007007 2013-08-30 15:07:07

0

我找到了答案! theSwitch.superview实际上返回UITableViewCell中的contentView。 所以,你应该更改为:

UITableViewCell *cell = (UITableViewCell *)theSwitch.superview.superview; 
相关问题