UIButton in UITableViewCell crashes when clicked

1

This should be simple, but I can't figure out what's wrong. I'm creating a tableview and I want a button that I can click to switch between checked and unchecked:

    UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:DiaperCellIdentifier];

if (cell == nil)
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:DiaperCellIdentifier] autorelease];
        wetButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        image = [UIImage imageNamed:@"unchecked_large.png"];
        CGRect frame = CGRectMake(150.0, 5.0, image.size.width, image.size.height);
        wetButton.frame = frame;
        [wetButton setBackgroundImage:image forState:UIControlStateNormal]; 
        [wetButton addTarget:self action:@selector(wetClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:wetButton];

Now when I click this button, I get a stack trace....[NSCFString scale]: unrecognized selector sent to instance.... thanks for any help.

- (void) wetClicked:(id)sender{
if (isWet) {
    isWet = NO;
    [wetButton setBackgroundImage:@"unchecked_large.png" forState:UIControlStateNormal];
} else {
    isWet = YES;
    [wetButton setBackgroundImage:@"checked_large.png" forState:UIControlStateNormal];
}

}

and here's the trace:

2011-03-09 10:19:57.124 InfantCare[64064:207] -[NSCFString scale]: unrecognized selector sent to instance 0x33be0

2011-03-09 10:19:57.240 InfantCare[64064:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString scale]: unrecognized selector sent to instance 0x33be0'

* Call stack at first throw:

(

0   CoreFoundation                      0x00f2dbe9 __exceptionPreprocess + 185

1   libobjc.A.dylib                     0x010825c2 objc_exception_throw + 47

2   CoreFoundation                      0x00f2f6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187

3   CoreFoundation                      0x00e9f366 ___forwarding___ + 966

4   CoreFoundation                      0x00e9ef22 _CF_forwarding_prep_0 + 50

5   UIKit                               0x003d1e7b -[UIImageView setImage:] + 250

6   UIKit                               0x004ea353 -[UIButton layoutSubviews] + 273

7   QuartzCore                          0x01d58451 -[CALayer layoutSublayers] + 181

8   QuartzCore                          0x01d5817c CALayerLayoutIfNeeded + 220

9   QuartzCore                          0x01d5137c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310

10  QuartzCore                          0x01d510d0 _ZN2CA11Transaction6commitEv + 292

11  QuartzCore                          0x01d817d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99

12  CoreFoundation                      0x00f0efbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27

13  CoreFoundation                      0x00ea40e7 __CFRunLoopDoObservers + 295

14  CoreFoundation                      0x00e6cbd7 __CFRunLoopRun + 1575

15  CoreFoundation                      0x00e6c240 CFRunLoopRunSpecific + 208

16  CoreFoundation                      0x00e6c161 CFRunLoopRunInMode + 97

17  GraphicsServices                    0x017cf268 GSEventRunModal + 217

18  GraphicsServices                    0x017cf32d GSEventRun + 115

19  UIKit                               0x0031642e UIApplicationMain + 1160

20  InfantCare                          0x00002228 main + 102

21  InfantCare                          0x000021b9 start + 53

)

terminate called after throwing an instance of 'NSException'

Program received signal: “SIGABRT”.

iphone
uitableview
uibutton
asked on Stack Overflow Mar 9, 2011 by DNewell • edited Mar 9, 2011 by DNewell

2 Answers

3

I think you need to use an UIImage instance as a parameter when you call the function setBackgroundImage:

[wetButton setBackgroundImage:@"unchecked_large.png" forState:UIControlStateNormal]; 

use instead:

[wetButton setBackgroundImage:[UIImage imageNamed:@"unchecked_large.png"] forState:UIControlStateNormal];
answered on Stack Overflow Mar 9, 2011 by Nava Carmon
2

The code you have posted is fine. The problem is most likely in your wetClicked: method, you are calling the scale method on an NSString.


Now that you've posted wetClicked: and the trace, I see the problem: You're passing a string instead of an image to setBackgroundImage:forState:. Try this instead:

- (void) wetClicked:(id)sender{
    if (isWet) {
        isWet = NO;
        [wetButton setBackgroundImage:[UIImage imageNamed:@"unchecked_large.png"] forState:UIControlStateNormal];
    } else {
        isWet = YES;
        [wetButton setBackgroundImage:[UIImage imageNamed:@"checked_large.png"] forState:UIControlStateNormal];
    }
}
answered on Stack Overflow Mar 9, 2011 by Anomie • edited Mar 9, 2011 by Anomie

User contributions licensed under CC BY-SA 3.0