简体   繁体   中英

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:

- (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. Are column1 and column2 buttons? If your MyCell is loaded from nib file, make sure IBOutlets correctly set up. And make sure that you load xib files correctly! Take a look at example of loading custom cells from xib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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