【问题标题】:UIButton in UITableViewCell crashes when clicked点击时 UITableViewCell 中的 UIButton 崩溃
【发布时间】: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


【解决方案1】:

我认为调用setBackgroundImage函数时需要使用UIImage实例作为参数:

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

改用:

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

【讨论】:

  • 纳瓦,就是这样!非常感谢!
  • 我遇到了类似的问题,在阅读完这篇文章后,我将 imageWithContentsOfFile 更改为 imageNamed 并修复了它。我猜imageWithContentsOfFile 已经被自动释放了。
【解决方案2】:

您发布的代码很好。问题很可能出在您的wetClicked: 方法中,您在NSString 上调用scale 方法。


既然您已经发布了wetClicked: 和跟踪,我看到了问题:您将字符串而不是图像传递给setBackgroundImage:forState:。试试这个:

- (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];
    }
}

【讨论】:

  • 我已经包含了方法 wetClicked: 和跟踪。当我在 wetClicked 的开头有一个断点时,在转储之前我永远不会到达它......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多