Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1064791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:43:16+00:00 2026-05-16T19:43:16+00:00

Here is my old question Link I ask a question before,But I put the

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:43:17+00:00Added an answer on May 16, 2026 at 7:43 pm

    From the error message, I suspect your cast is wrong:

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

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this similar question here , but this is really old. Was it
I saw the next two methods in an old question here but it is
i know, this is an old question but i can't get this to work...
NOTE: This is an old question and the answers here no longer works (since
here is my old code which works: $('.logo').hover(function(){ $(this).addClass('animated swing')}, function(){$(this).removeClass('animated swing') }); here
Here is an old add in to quickly comment out HTML code. Maybe I
Here is an excerpt from my code: xtype: 'image', src: 'http://www.sencha.com/files/blog/old/blog/wp-content/uploads/2010/06/sencha-logo.png', left: '50%', top:
My site is up here: Green McP I'm trying to put my old WP
Keeping the old question. See below for resolution. It is probably something simple, but
I'm sure this is a very old question, but I could not find a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.