Crash when trying to load a Custom NIB

Discussion in 'iOS Programming' started by John Baughman, Mar 30, 2011.

  1. macrumors member

    Joined:
    Oct 27, 2003
    #1
    I cannot figure this one out. I have a created a custom UITableViewCell in IB. As far as I can tell I have all the objects in the cell which are all UILabels wired up properly and everything builds without any errors. When I try to use the custom cell the app stops when I try to load the NIB.

    I have double checked the NIB to make sure that I have the class for the cell set to my custom subclass (ArrivalTimesCell) and rechecked to make sure that the 4 labels in the cell are properly connected to the correct IBOutlets in the interface file (file's owner).

    I've listed what I have in place for this below.

    Any help or hints or where to look to figure this one out would be appreciated.

    Thanks,

    John

    Here are my UITableViewCell interface and implementation methods...

    Code:
    //  ArrivalTimesCell.h
    
    #import <UIKit/UIKit.h>
    
    @interface ArrivalTimesCell : UITableViewCell {
        IBOutlet UILabel *cityArrivalTimeLabel;
        IBOutlet UILabel *optimumArrivalTimeLabel;
        IBOutlet UILabel *arrivalAvoidTimesLabel;
        IBOutlet UILabel *arrivalTimeNotSetLabel;
    
    }
    @property (nonatomic, retain)IBOutlet UILabel *cityArrivalTimeLabel;
    @property (nonatomic, retain)IBOutlet UILabel *optimumArrivalTimeLabel;
    @property (nonatomic, retain)IBOutlet UILabel *arrivalAvoidTimesLabel;
    @property (nonatomic, retain)IBOutlet UILabel *arrivalTimeNotSetLabel;
    
    @end
    
    //  ArrivalTimesCell.m
    
    #import "ArrivalTimesCell.h"
    
    @implementation ArrivalTimesCell
    @synthesize arrivalTimeNotSetLabel, arrivalAvoidTimesLabel, optimumArrivalTimeLabel, cityArrivalTimeLabel;
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
    
        }
        return self;
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated{
        [super setSelected:selected animated:animated];
    
    }
    
    - (void)dealloc{
        [super dealloc];
        [arrivalTimeNotSetLabel release];
        arrivalTimeNotSetLabel = nil;
        [arrivalAvoidTimesLabel release];
        arrivalAvoidTimesLabel = nil;
        [optimumArrivalTimeLabel release];
        optimumArrivalTimeLabel = nil;
        [cityArrivalTimeLabel release];
        cityArrivalTimeLabel = nil;
    }
    
    @end
    Here is a portion of cellForRowAtIndexPath for the UITableView...

    Code:
    //  TripEditViewController.m
    
    #import "ArrivalTimesCell.h"
    
    @implementation TripEditViewController
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        CellIdentifier = @"ArrivalTimesCell";
        ArrivalTimesCell *cell = (ArrivalTimesCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if(cell == nil){
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ArrivalTimesCell" owner:nil options:nil];
    
            for (id currentObject in topLevelObjects) {
                if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                    cell = (ArrivalTimesCell *) currentObject;
                    break;
                 }
             }      
        }
    
    
        cell.cityArrivalTimeLabel.text = @"Whatever"
        cell.arrivalAvoidTimesLabel.text = @"Whatever";
        cell.optimumArrivalTimeLabel.text = @"Whatever";
        cell.arrivalTimeNotSetLabel.text = @"Whatever";
    
        return cell;  
    }

    Program execution stops on the loadNibNamed line without any comment in the console. If I "step into" I get the following which I am not sure has anything to do with the cause of my crash but instead is the result of the nib not loading properly. As you can see about I synthesized the 4 keys one of which is noted in this error message. That is the first labelI set, and if I comment it out the next key set shows up in this error message. I think this makes sense if the NIB is not loaded and I step into the firs key being set.

     
  2. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Well, go into your nib, and check if there is still a label left hooked up to a label, that no longer exists ;)
    called "cityArrivalTimeLabel". this is what happened to me several times when using custom Cells.
     
  3. thread starter macrumors member

    Joined:
    Oct 27, 2003
    #3
    I have done that over and over again, rewiring the labels, checking to be sure that I have properly set their properties and synthesized them. I deleted the whole thing and rewrote the subclass and it's NIB. If it really is a problem with cityArrivalTimeLabel then all the labels are not wired or implemented correctly because if I comment out any reference to cityArrivalTimeLabel after loading the NIB, then I get the same error for the next label referenced and the next if I comment out that one.

    I can see that the error calls the label as a problem, but why does the program halt on the line that loads the NIB, before it gets to any reference to a label? If the problem is the labels then this is not how the version prior to xCode 4 worked. It would crash somewhere just beyond the offending line line and you had to go back in in the call chain to actually see the code that was causing the problem.
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    The cityArrivalTimeLabel is a property on the ArrivalTimesCell. Is it connected to the File's Owner in the nib or to the cell in the nib? I bet you connected it to File's Owner, which is wrong.
     
  5. thread starter macrumors member

    Joined:
    Oct 27, 2003
    #5
    Thanks. That corrected the key pair problem. I had tried them both ways, but did not notice that the error had changed and thought I was still dealing with the same issue. With the labels properly connected, as you pointed out, to the cell and not the File's Owner everything works until the cell is returned. It crashes with...

    So I still do not have it setup correctly. A quick question.. Should the File's Owner class be set to the viewcontoller with the UITableView or to the ArrivalTimesCell. I tried it both ways but it did not make any difference.

    I have not done anything else in the UITableView's controller other than import ArrivalTimesCell.h. Do I need to wire anything else to the NIB?

    Thanks in advance for any additional help.

    John
     
  6. thread starter macrumors member

    Joined:
    Oct 27, 2003
    #6
    I got it fixed.

    There are actually 2 tables being handled by the view controller with an if statement in the cellForRowAtIndexPath and I was returning the cell with a single call outside of the if statement. I changed this to return the cell for each table inside the if statement and it is now working great. Thanks for your help!
     

Share This Page