Search code examples
objective-cuitableviewxib

Loading UITableViewCell from xib results in loaded the nib but the view outlet was not set


I'm trying to create a UITableViewController that it's UITableViewCell is a xib file. The Xib file is empty (0 controls) all the controls are created programmatically like this

- (id) initWithTitle: (NSString*)p_title andImage: (UIImage*)p_image
{
    //adding checkbox & title
    MMCheckbox* myCheckbox = [[MMCheckbox alloc] initWithTitle:p_title andHeight:20];
    myCheckbox.frame = CGRectMake(self.frame.size.width - myCheckbox.frame.size.width -15,20, myCheckbox.frame.size.width, 20);
    myCheckbox.flat = YES;
    myCheckbox.strokeColor = [UIColor whiteColor];
    myCheckbox.checkColor = [UIColor magentaColor];
    myCheckbox.uncheckedColor = [UIColor clearColor];
    myCheckbox.tintColor = [UIColor clearColor];
    [self addSubview:myCheckbox];

    //adding the image
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake (15, 10, 20, 20)];
    [imageView setImage: p_image];

    return self;
}

In the xib file I placed a UITableViewCell and set it's class to my class MMCheckboxTableViewCell

enter image description here

In my tableViewController I'm creating the cell loading it from NIB like so

- (UITableViewCell *)tableView:(UITableView *)p_tableView cellForRowAtIndexPath:(NSIndexPath *)p_indexPath
{
//create a UI tableViewCell
    UITableViewCell* cell = [p_tableView
                             dequeueReusableCellWithIdentifier:@"comboBoxCell"];
    if(cell == nil)
    {
        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"MMComboBoxTableCell" bundle:nil];
        // Grab a pointer to the custom cell.
        cell = (MMComboBoxTableCell *)temporaryController.view;
    }
}

I've looked at many post with the same question and checked to see I've got every thing right according to this.

Please let me know what I'm missing.


Solution

  • My problem was with the way I was loading the XIB.

    By changing the following:

    UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"MMComboBoxTableCell" bundle:nil];
    

    to this:

    MMComboBoxTableCell* temporaryController = [[[NSBundle mainBundle] loadNibNamed:@"MMComboBoxTableCell" owner:self options:nil] objectAtIndex:0];
    

    Problem solved.