简体   繁体   English

自定义UITableViewCell按钮操作未调用?

[英]Custom UITableViewCell button action not calling?

I have a custom cell with a button inside. 我有一个带有按钮的自定义单元格。 My problem is action method is not calling when click on cell button 我的问题是单击单元格按钮时动作方法未调用

Right now I'm doing 现在我在做

 UITableViewCell *cell = [self.tblHomeView dequeueReusableCellWithIdentifier:MyIdentifier];
    MyCell * mycell2 = ((MyCell*)cell);
    if (cell == nil) {
        cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];

        [mycell2.column1 addTarget:self action:@selector(column1Selected:) forControlEvents:UIControlEventTouchDown];
        [mycell2.column2 addTarget:self action:@selector(column1Selected:) forControlEvents:UIControlEventTouchDown];        
    }

In my cellForRowAtIndexPath method and my method is: 在我的cellForRowAtIndexPath方法中,我的方法是:

- (void) column1Selected: (id) sender
{
    UIAlertView *alert = [[ UIAlertView alloc]                          
                          initWithTitle: @" Alert"                          
                          message: [NSString stringWithFormat: @"button %d",((UIButton *) sender).tag]
                          delegate: nil                          
                          cancelButtonTitle: @" OK"                          
                          otherButtonTitles: nil];    
    [alert show] ;
    [alert release];
}

Any tips? 有小费吗? Thank's 谢谢

Seems that you should send message addTarget:action:forControlEvent: to instance of UIButton, but you are sending it to something different. 似乎您应该将消息addTarget:action:forControlEvent:发送到UIButton的实例,但是您将其发送给其他对象。 Are column1 and column2 buttons? column1column2按钮吗? If your MyCell is loaded from nib file, make sure IBOutlets correctly set up. 如果您的MyCell是从nib文件加载的,请确保正确设置了IBOutlets。 And make sure that you load xib files correctly! 并确保正确加载xib文件! Take a look at example of loading custom cells from xib 看一下从xib加载定制单元的示例

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

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