简体   繁体   中英

iOS - Loading custom table cell results in error on second load

I am working on an iPad app with a table view with standard cells, when a custom cell is selected, it should expand and load a custom nib file. This it does fine, for the first selection.

If I select a standard cell it loads the nib fine and if I select it again it goes back to normal, upon the second loading, it throws a EXC_BAD_ACCESS error (I don't think I will ever get xcodes errors, seem to be the most abstract).

My code is below and the line is when it dequeues the cell for reuse, 3rd line:

if([listCells objectAtIndex:indexPath.row] == @"open") {
        NSLog(@"Loading open cell at %i", indexPath.row);
        CustomMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomMessageCell"];
        //Loads the nib file and grabs the last object, presumably the table cell, as it is the only object in the file.
        if(cell==nil) {
            cell = [[[[NSBundle mainBundle] loadNibNamed:@"CustomMessageCell" owner:self options:nil] lastObject] autorelease];
        }
        UILabel *message = (UILabel *) [cell viewWithTag:1];
        UIButton *approve = (UIButton *)[cell viewWithTag:4];
        message.text = @"Test";
        return cell;

Any help is greatly appreciated, thanks!

Have you compiled and run on a real device? Sometimes you get more info then running just on the simulation.

Also other things to check:

  • you should check that the Identifier of the Nib file in IB is set correctly to CustomMessageCell.
  • Be sure that your subviews identifiers in IB are set correctly.
  • Note that you should not autorelease the cell as it is already autoreleased as returned by the lastObject method. This could cause another reason for crash.

Try also running your app (on a real device) and profile it using the Zombies and then Leaks profiling tools. (Instead of just "Run" choose "Profile").

Hope this helps.

You shouldn't autorelease cell in this line:

cell = [[[[NSBundle mainBundle] loadNibNamed:@"CustomMessageCell" 
                                 owner:self options:nil] lastObject] autorelease];

Just create like this:

cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomMessageCell" 
                               owner:self options:nil] lastObject];

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