【发布时间】:2011-03-09 17:21:30
【问题描述】:
这应该很简单,但我不知道出了什么问题。我正在创建一个表格视图,我想要一个可以单击以在选中和未选中之间切换的按钮:
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];
现在,当我单击此按钮时,我得到一个堆栈跟踪...[NSCFString scale]: unrecognized selector sent to instance...。感谢您的帮助。
- (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];
}
}
这是跟踪:
2011-03-09 10:19:57.124 InfantCare[64064:207] -[NSCFString scale]:无法识别的选择器发送到实例 0x33be0
2011-03-09 10:19:57.240 InfantCare[64064:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSCFString scale]:无法识别的选择器发送到实例 0x33be0'
* 首次抛出时调用堆栈:
(
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
)
在抛出 'NSException' 实例后调用终止
节目接收信号:“SIGABRT”。
【问题讨论】:
-
按钮添加为子视图后无需保留。至少除非您以后需要访问它。除此之外,您发布的代码对我来说还不错。我认为比例是 UIImage 的一个属性。如果这个 get 被发送到一个字符串,你可能在代码中的某个地方有内存泄漏。您是否使用仪器检查了应用程序是否泄漏?查看完整的堆栈跟踪也可能会有所帮助。
-
请发布您的
wetClicked:方法,问题很可能在其中。
标签: iphone uitableview uibutton