UITableViewCell from custom .XIB file does not create points

Update 2:

In short, I was stupid (and in my opinion not educated enough) about it. He is working now. My questions went a bit closer to the original topic, but that's because I didn't understand what was going on in my application. I would like to close the question with one last (and small) query:

I have two labels in my customCell.xib. I want one of them ( cell.label2

) to sometimes contain a longer segment of text (2-3 lines). I know one way to make it all fit is to set the autoshrink property, but that shrinks to text so it can fit on one line. I want to keep the original size of the text and instead increase the height of the cell, making the text span across multiple lines, rather than shrinking it.

Is there a way to do this?

Update 1:

I tried several things based on the answers below and they didn't get me anywhere. I'm becoming more and more convinced that I'm doing something fundamentally wrong, and you guys just can't think about it because it's that simple. So try again. I will change the names a little so they are easier to remember.

The problem is that I cannot create IBOutlets

or IBActions

for my customCell. Right now I have 3 files that need to handle this (DetailDirectionViewCell. {H, m, xib}, but the Interface Builder won't let me create a property / exit / reference from my object UITableViewCell

- anywhere.

Instead of copying the code here, I've provided a PasteBin entry with links to my code. As before, I removed the less interesting methods. Take a look if you like. http://pastebin.com/p4eADhKQ

I also have a customCell. {h, m}, but these are only new Objective C class files that inherit from UITableViewCell

. customCell.xib

is only a cell with two labels.

So, I have a couple of problems.

First, creating a UITableView

programmatically using the custom UITableViewCell

one contained in the .XIB file. The DirectionsViewController class is just UITableView

about programmed cells. Clicking on one of the cells should present the DetailedDirectionsViewController table (modally), the design of the cell for which the DetailedDirectionsViewCell.xib file is located. The problem is, I can't create IBOutlet

for UITableViewCell

from a nib file - anywhere. Dragging and dropping the file owner icon / dropdown doesn't prompt to create anything. Which after 5 hours of fighting means that I cannot provide my detailed review.

The second problem is adding the navigation bar to the DirectionsViewController, but let's leave it alone.

Here are some ways that might help you:

//inside DirectionsViewController
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)
    DetailedDirectionsViewController *vc = [[DetailedDirectionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    vc.instruction = [turnList objectAtIndexPath:indexPath.row];
    [self.tabBarController presentModalViewController:vc animated:YES];

//inside DetailedDirectionsViewController
- (UITableViewCell *) tableView:(UITableView *) cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"directionsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        [[NSBundle mainBundle] initWIthNibNamed:@"DetailedDirectionsViewCell" owner:nil options:nil];
        cell = self.tableViewCell;
        self.tableViewCell = nil;

    //here I configure the custom cell
    return cell;


I don't think the rest of the methods are of interest because they either work as expected or are pretty much standard.


DirectionsViewController is essentially a UITableViewController

with custom cells. No .xib file Detailed DescriptionViewController - Detailed entry information from DirectionsViewController. The cells here should come from an .XIB file, but that's broken. DetailedDirectionsViewCell is a custom cell. I cannot establish its owner of the file.


source to share

4 answers

OK. You are not creating a connection IBOutlet

to the File Owner. Take a look at the screenshot. You are creating IBOutlet

from a view a CustomCell (with a red arrow).

After looking behind the code, do the following.

1) Open the file CustomCell.h

. As you say customCell.xib

has two UILabel

s (suppose label1 & label2

) you will need to declare properties and create outputs in a file CustomCell.h

and in a .m file to synthesize and release it. Refer to this code screen.

enter image description here

2) Now in customCell.xib

, select the view CustomCell

, not the owner of the file (the owner of the file should only inherit from NSObject

, go to the Identity Inspector (marked with a red ellipse) and select the appropriate Customcell class (marked with a red rectangle).

3) Right click your custom cell view and make shortcut connections. And save it.

enter image description here

4) In yours DirectionsViewController.m

, you have this UITableView delegate method cellForRowAtIndexPath

. Change it like this:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 static NSString *CustomCellIdentifier = @"CustomCell";
EditProjectCustomCell *cell = (EditProjectCustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; // typecast to customcell

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

if (cell == nil)
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EditProjectCustomCell" owner:self options:nil];

        for (id oneObject in nib) 
            if ([oneObject isKindOfClass:[EditProjectCustomCell class]])

            cell = (EditProjectCustomCell *)oneObject;

        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];


cell.label1.text=[someArray1 ObjectAtIndexPath: indexPath.row];
cell.label2.text=[someArray2 ObjectAtIndexPath: indexPath.row];

return cell;



this delegate method will be called as many times as you have returned a value to numberOfRowsInSection

the DataSource method. Every time cell.label is empty and you add data to the label by calling this line. so no need to create a shortcut every time you did between line 79-90 here. http://pastebin.com/Vd4PKjTu

 cell.label1.text=[someArray ObjectAtIndexPath: indexPath.row];


Creating a custom cell means that you yourself create the user interface (e.g. .xib), interface and implementation (.h, .m file) for UITableViewCell

and apply them in your class (i.e. DirectionsViewController.m

) cellForRowAtIndexPath

the delegate method.



To load custom cell i used this code (test it and it works)

static NSString *CustomCellIdentifier = @"CustomCommentIdentifier";
detailedDirectionsViewCell *cell = (DetailedDirectionsViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailedDirectionsViewCell" owner:self options:nil];
    for (id oneObject in nib)
             if ([oneObject isKindOfClass:[DetailedDirectionsViewCell class]])
                  cell = (DetailedDirectionsViewCell *)oneObject;


and make sure you change the cell file nib file like this:

  • Click on the owner of the file, change its type to NSObject

    (this should not get confused when connecting Outlets.
  • Remove any view you have in the nib file, then drag the component UITableViewCell

  • Change the superclass of the component to the Cell class, in your case change the cell from UITableViewCell

    to DetailedDirectionsViewCell

  • Plug in the outlet.

This should work, let me know if you have any questions.



To answer your last question in Update 2, did you consider using a textbox instead of a label? You must be able to disable editing so that the user cannot change what is displayed. If you need it to be a shortcut, there is a Lines property in the Shortcut dropdown in the Label Attribute Inspector that you can customize. I'm not sure how to access this property programmatically, but a quick google search should get you there.



In my case, I had two different views for the same cell class. And when I connected the outputs to the .h file, they were only connected to the view of the first view, not the second. To make sure your outlets are actually connected, go to the Xcode me menu and open View -> Utilities -> Show FileConnection, then check that your viewpoints are actually connected.

enter image description here



All Articles