【问题标题】:Program Crash when I click a switch?单击开关时程序崩溃?
【发布时间】:2010-09-15 07:50:56
【问题描述】:

这是我的老问题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;

我需要每个单元格都是图像,文本标签,内部切换

在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);
    }
}

崩溃消息是“Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[LightCell0 indexPathForCell:]: unrecognized selector sent to instance”

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

【问题讨论】:

    标签: iphone uitableview uiswitch


    【解决方案1】:

    从错误消息中,我怀疑你的演员表是错误的:

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

    你确定theSwitch的superview是UITableViewCell,cell的superview是UITableView吗?崩溃告诉您,您尊重的 tableView 对象是 LightCell0 对象

    【讨论】:

    • 我不确定.......因为我只是参考答案并直接复制到我的代码中......
    • Vodkhang 是正确的,您的逻辑/设计在某些时候是错误的。开关的“superview”的“superview”是你的单元格,而不是你期望的tableview
    • 这不是我的答案...只是有人告诉我这可以工作...所以我复制了他的答案。
    • 所以,这个答案不起作用。为什么需要获取 indexPath?目的是什么
    • 我觉得你需要做 theSwitch.superView.superView.superView 3次,试试那个
    【解决方案2】:

    为了获取开关的状态,您使用的是“yourSwitch.on”,我认为这是不正确的。 将其更改为 [yourSwitch isOn] 并测试您的应用程序。 我在您的代码中看到 2 条语句,您正在检查开关的状态。更改它们并尝试。

    【讨论】:

    • 但还是不知道点击哪个开关?
    • 为什么不给交换机一个标签,并根据标签创建一个新的NSIndexPath:NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2017-09-27
      • 2015-04-26
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多