0

I am writing an app in iOS 6. This is a snippet of code from ViewController.m file:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier"];

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        cell = _customCell;
        _customCell = nil;

    }

    cell.firstName.textLabel = @"dsdsds";
    cell.middleName.textLabel = @"N/A";
    cell.lastName.textLabel = @"daasdsdasa";

    return cell;
}

These lines of code give me error (Property 'firstName' not found on object of type 'CustomCell*'):

cell.firstName.textLabel = @"dsdsds";
        cell.middleName.textLabel = @"N/A";
        cell.lastName.textLabel = @"daasdsdasa";

CustomeCell.h:

#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *firstName;

@property (strong, nonatomic) IBOutlet UILabel *middleName;
@property (strong, nonatomic) IBOutlet UILabel *lastName;

+(NSString*) reuseIdentifier;
@end

In the Outlets of CustomCell.xib:
firstName -> label
middleName -> label
lastName -> label

Referencing Outlets:
customCell -> File's Owner

Selecting the firstName label:
Referencing Outlets:
firstName -> CustomCell
firstName -> CustomCell -CustomCellIdentifier

Selecting the middleName label:
lastName -> Custom Cell - CustomCellIdentifier
middleName -> Custom Cell

Selecting the lastName label:
lastName -> Custom Cell
middleName -> Custom Cell- Custom Cell Identifier

So, what is the prob? In my opinion it has something to do with Outlets.

uml
  • 1,171
  • 4
  • 16
  • 28

3 Answers3

1

The error (Property 'firstName' not found on object of type 'CustomCell*'): means that the compiler does not know about a property by the name of firstName. You need to inform the compiler what properties are available within a class, typically by importing the header file.

So, at the top of the file where your table view code is, put:

#import "CustomCell.h"

(Note that it goes before your @implementation block, up with the other #import's.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
1

I saw a couple of mistakes in your code:

cell.firstName.textLabel = @"dsdsds";
cell.middleName.textLabel = @"N/A";
cell.lastName.textLabel = @"daasdsdasa";

The issue is there is no property like textLabel for UILabel.

Change it to:

cell.firstName.text  = @"dsdsds";
cell.middleName.text = @"N/A";
cell.lastName.text   = @"daasdsdasa";

If you not synthesized the property use the following instead:

cell._firstName.text  = @"dsdsds";
cell._middleName.text = @"N/A";
cell._lastName.text   = @"daasdsdasa";

Also change:

if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
}

to

if (cell == nil) {
         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = (CustomCell *)[nib objectAtIndex:0];
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • According to XCODE text has been deprecated, I shall use textLabel. The resy fix is not helping either. – uml Jan 30 '13 at 05:21
  • @uml: that is for cell. Cell.text is deprecated so you need to use cell.textLabel. There is no property like textLabel for UILabel – Midhun MP Jan 30 '13 at 05:24
  • @uml: that was a typo. please check it – Midhun MP Jan 30 '13 at 05:37
  • Nope. Same errors. I followed your suggestion regarding naming and so on – uml Jan 30 '13 at 05:47
  • @uml: did you changed to _firstName ? – Midhun MP Jan 30 '13 at 05:48
  • He is using iOS 6.0 which has auto-synthesis, therefore he has setters and getters.... – lnafziger Jan 30 '13 at 05:54
  • @Inafiziger: in iOS 6 if you not synthesized your property iOS will wutomatically synthesize that in the form of `_yourIvar` – Midhun MP Jan 30 '13 at 05:56
  • Yes, and therefore he shouldn't be using cell._firstName. He should use cell.firstName as he is... Plus in your answer you say "If you not synthesized the property". (ergo my previous comment) – lnafziger Jan 30 '13 at 05:56
  • auto-synthesize is a function of the compiler, not the OS. Newer XCode is just injecting that code for us. – Dave Jan 30 '13 at 06:00
  • @Dave: Yes, but if he is targeting iOS 6 then his compiler supports auto-synthesis... – lnafziger Jan 31 '13 at 06:09
0

why do you have the class method +reuseIdentifier in your .h file? The UITableViewCell class that you inherit from has a property with that name, so that's a tad confusing.

Also, the convention is to use "weak" for your outlets: @property (weak, nonatomic) IBOutlet UILabel *firstName; because the view will have a strong copy of it's subviews.

I'm not sure why your property isn't being found. It's a runtime error. If things were not wired up in the nib correctly; you'd get an exception as soon as you try to instantiate the that view. Did you set the "class" of that view to CustomCell? (Though if you didn't you should be dequeuing a regular TableViewCell and wouldn't have been able to set your outlets, so...) Hmm.

Dave
  • 7,552
  • 4
  • 22
  • 26
  • Weak is not allowed in ARC and GC. An error received once switching to weak and performing synthesizing. The class for .xib has been set to CustomCell – uml Jan 30 '13 at 05:18
  • 1
    There's no GC for iOS; and strong/weak are specifically for ARC. If you're targeting older iOS; you can sometimes use unsafe_unretained in lieu of weak. Anyway, good luck with your issue. – Dave Jan 30 '13 at 05:35