簡體   English   中英

單擊時,UITableViewCell中的UIButton崩潰

[英]UIButton in UITableViewCell crashes when clicked

這應該很簡單,但是我不知道出了什么問題。 我正在創建一個tableview,我想要一個可以單擊以在已選中和未選中之間切換的按鈕:

    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]:無法識別的選擇器發送到實例....感謝您的幫助。

- (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”。

我認為您在調用函數setBackgroundImage時需要使用UIImage實例作為參數:

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

改用:

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

您發布的代碼很好。 問題很可能是在您的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];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM