1

これが私の古い質問ですリンク

私は前に質問をします、しかし私は私のコードに答えを入れましたそれはエラーなしでコンパイラーすることができます

しかし、スイッチをクリックすると、プログラムがクラッシュします

クリックした行を返すよりも、スイッチをクリックしたいだけです

これが私のコードです

まず、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

誰かが私のコードで何が起こっているのか知っていますか?

4

3 に答える 3

1

エラーメッセージから、キャストが間違っていると思われます。

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

SwitchのスーパービューがUITableViewCellであり、セルのスーパービューがUITableViewであることを確認してください。クラッシュは、尊重するtableViewオブジェクトがLightCell0オブジェクトであることを示しています

于 2010-09-15T08:22:28.610 に答える
0

スイッチのステータスを取得するには、「yourSwitch.on」を使用していますが、これは正しくないと思います。変更し[yourSwitch isOn]てアプリをテストします。スイッチの状態をチェックしているコードに2つのステートメントがあります。それらを変更してみてください。

于 2010-09-15T09:33:17.423 に答える
0

私は答えを見つけました!theSwitch.superviewは、実際にはUITableViewCell内のcontentViewを返します。したがって、次のように変更する必要があります。

UITableViewCell *cell = (UITableViewCell *)theSwitch.superview.superview;
于 2010-10-23T11:30:47.773 に答える