简体   繁体   中英

EXC_BAD_ACCESS When I click a button inside a custom UITableViewCell

I build a TableView with custom TableViewCell, the first custom cell have a tool bar and some Bar button Item and also a simple button for test. The problem is : When I click in any bar button item or into simple button I have a EXC_BAD _ACCESS ?

This is my code to build the tableview cells :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"taskCell";

    if(indexPath.row != 0){

        TaskCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"TaskCell" owner:nil options:nil];

            for (UIView *view in views) {
                if([view isKindOfClass:[UITableViewCell class]])
                {
                    cell = (TaskCell*)view;
                }
            }
        }
        return cell;
    }
    else{

        if (travelInfoCell == nil) {

            NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"TravelInfo" owner:nil options:nil];

            for (UIView *view in views) {
                if([view isKindOfClass:[UITableViewCell class]])
                {
                    travelInfoCell = (TravelInfo*)view;
                }
            }
        }

        travelInfoCell.selectionStyle = UITableViewCellSelectionStyleNone;

        return travelInfoCell;
    }

}

I planed to delegate the action code to the TableViewController, but for the instant I can not even hit the breakpoint in front of the button IBAction.

Code into the TravelInfo.m and is not working and throw to me EXC_BAD _ACCESS :

- (IBAction)doAccepted:(id)sender {
    NSLog(@"accepted");
    //[delegate travelAccepted];
}

Do have any solution ?

Here's your problem:

travelInfoCell = (TravelInfo*)view;

You are not keeping a reference to the view, so there is an implicit limited life expectancy to the view variable. You'll need to retain to keep a reference. Do this:

travelInfoCell = [(TravelInfo*)view retain];

Don't forget to release the cell and nil it out on viewDidUnload

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