简体   繁体   English

在点击时,UIButton在UItableview中创建单元格崩溃

[英]UIButton created in UItableview cell crashes when tapped

I created a custom UItableviewcell 我创建了一个自定义UItableviewcell

added a button into its initWithStyle area: 在initWithStyle区域添加了一个按钮:

favoriteButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
        favoriteButton.backgroundColor = [UIColor clearColor];
        [favoriteButton setTitle:@"Play" forState:UIControlStateNormal];
        [favoriteButton addTarget:self action:@selector(favoriteClick:) forControlEvents:UIControlEventTouchUpInside];
        favoriteButton.center = self.center;

set its frame and layout in: 将其框架和布局设置为:

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;
    CGRect frame;

    frame = CGRectMake(boundsX+200, 44, 40, 40);
    favoriteButton.frame = frame;

}

and then in my tableviewcontroller.m file I use that tableviewcell and have the user click tap to go to the action within that tableviewcontroller.m code 然后在我的tableviewcontroller.m文件中,我使用该tableviewcell并让用户点击以转到该tableviewcontroller.m代码中的操作

-(void)favoriteClick:(id)sender// forEvent:(UIEvent*)event
{
    NSLog(@"click");

}

however when i tap the button I get an unhandled exception: 但是当我点击按钮时,我得到一个未处理的异常:

2010-07-24 16:06:02.197 Film Fest[10342:40b] -[ProgramGuideCell favoriteClick:]: unrecognized selector sent to instance 0x6e63ff0
2010-07-24 16:06:02.200 Film Fest[10342:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ProgramGuideCell favoriteClick:]: unrecognized selector sent to instance 0x6e63ff0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02a56919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x02ba45de objc_exception_throw + 47
    2   CoreFoundation                      0x02a5842b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x029c8116 ___forwarding___ + 966
    4   CoreFoundation                      0x029c7cd2 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x00301e14 -[UIApplication sendAction:to:from:forEvent:] + 119
    6   UIKit                               0x0038b6c8 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x0038db4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    8   UIKit                               0x0038c6f7 -[UIControl touchesEnded:withEvent:] + 458
    9   UIKit                               0x00579070 _UIGestureRecognizerUpdateObserver + 3687
    10  CoreFoundation                      0x02a37d1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    11  CoreFoundation                      0x029cc987 __CFRunLoopDoObservers + 295
    12  CoreFoundation                      0x02995c17 __CFRunLoopRun + 1575
    13  CoreFoundation                      0x02995280 CFRunLoopRunSpecific + 208
    14  CoreFoundation                      0x029951a1 CFRunLoopRunInMode + 97
    15  GraphicsServices                    0x030b42c8 GSEventRunModal + 217
    16  GraphicsServices                    0x030b438d GSEventRun + 115
    17  UIKit                               0x0030fb58 UIApplicationMain + 1160
    18  Film Fest                           0x00002170 main + 102
    19  Film Fest                           0x00002101 start + 53
)
terminate called after throwing an instance of 'NSException'

I dont see what Im doing wrong here... 我不知道我在这里做错了什么......

Try returning IBAction instead of void on your selector, and put the selector in the ProgramGuideCell instead of tableviewcontroller. 尝试在选择器上返回IBAction而不是void ,并将选择器放在ProgramGuideCell而不是tableviewcontroller中。

-(IBAction)favoriteClick:(id)sender// forEvent:(UIEvent*)event
{
    NSLog(@"click");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM