views:

49

answers:

1

Hi I am calling

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

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        //cell = [[[CustomCell alloc] initwithidentifier:CustomCellIdentifier] autorelease];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)oneObject;
            }
    }

    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.listData objectAtIndex:row];
    cell.nameLabel.text = @"text";
    [cell.contentView addSubview: nameLabel];
    [cell reloadInputViews];

    return cell;
}

But the nameLabel wont display with its new name. What am I doing wrong please?? Thanks!

+1  A: 

I'm willing to guess that if you inspect cell.NameLabel, you'll find that it's nil — the IBOutlet hasn't been wired up correctly by loadNibNamed:owner:options:. Are you sure that CustomCell has nameLabel as an appropriate outlet (ie, one that can be reached by key-value coding)? Apple's reference for that is here, but ensuring you have a setter and getter is sufficient.

As you've created your NIB, you're probably right that CustomCell isn't the file owner? If it is then loadNibNamed:owner:options: obviously shouldn't be called on self. And if 'self' is the actual owner, it may be neater to have an IBOutlet (also definitely KVC compliant) that points to the CustomCellView rather than fishing through the returned array.

ADDITIONS, subsequent to the comment discussion below:

What definitely works is to create a custom subclass of UITableViewCell with suitable IBOutlets that are also properties, to set your table view data source as the file owner and to use loadNib to load a new instance of the NIB with your class as the target, causing your outlet to have a new instance attached. Then you can use the properties of the subclass to access various subviews of the cell. Sample project: here.

Tommy
no I did create the appropriate outlet for the label. THe cell appears - without any values though
Lily
The outlet won't be loaded unless the relevant member is KVC compliant, regardless of whether you created it, and the view will display even without an outlet. I strongly recommend you check whether you're getting nil for cell.NameLabel.
Tommy
I don't know if I am being clear, the cell appears with all of the custom objects within it. In other words, I have a button, a textview, a label and an image. All of these items are nil however. Since these are appearing the Outlet has been set up correctly, right?
Lily
Not necessarily — outlets just give one object a handle to another. An object can exist without being pointed to by any outlets. When a NIB is loaded the various objects will be loaded and instantiated first and the outlet links will be set second. Objects will be loaded even if there are no outlets pointing to them and will remain loaded even if setting the outlets fails.
Tommy
Oh thanks for the explanation. How may I check that there is an outlet for the data to go into the objects? thanks!
Lily
One way is to chuck in an NSLog(@"Something memorable: %@", cell.nameLabel); on the line just above where you set the .text property to @"text", then check the console. If it prints a description of a UILabel object then the outlet is working fine and the problem is something else, but if it prints (nil) then the outlet isn't being set up correctly.
Tommy
you are very right. It returned null :S
Lily
I really don't understand how I am not hooking it up right.
Lily
what I don't understand is that when I am debugging and it reaches the label section, it actually goes to the part of the code where the objects are synthesized.
Lily
You may be hooking it up right just for the connection not to be settable at runtime. Do you definitely have a setter for the relevant property? It can be via an @property/@synthesize pair or just as a method.
Tommy
In the custom cell class i have @property and @synthesize both set up
Lily
Then I'm not immediately sure what to advise. I'll take a moment to try some actual code over the weekend; in the meantime, if this is really holding you up then a solution that skirts the problem is to give all of the UIViews you want to be able to write to unique tags (it's a numeric value in Interface Builder, near the tickboxes for hidden/opaque/etc in the inspector) and use viewWithTag: on the parent view to get the subview you want quickly.
Tommy
Thank you so much! I think I am attaching the UILabel to the File Owner instead of the custom cell! Will check that and see if that is the problem.
Lily
I'm not sure if that would actually be a problem, but I tried wiring the UILabel directly from the cell and everything worked correctly. See the demo project I've just added to the answer.
Tommy