4

I have implemented my own UITableViewCell and created a .xib for it. I have connected all the labels using a strong IBOutlet and initialized the UILabels in the awakeFromNib() method. However, whenever I run the iOS Simulator, I run into an issue where the UILabel is (null) in NSLog.

I am wondering if it has to do with how I am loading in the text for the UILabel. I have tried to create a shortened version of the project below that outlines the issue I'm running into.

I would also like to note that I can click on the actual rows, but that there is still no text displaying.

My code:

ToDoCell.h

#import <UIKit/UIKit.h>

@interface ToDoCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *minutesLeft;
@property (strong, nonatomic) IBOutlet UILabel *hoursLeft;
@property (strong, nonatomic) IBOutlet UILabel *daysLeft;
@property (strong, nonatomic) IBOutlet UILabel *taskLabel;

@end

ToDoCell.m

#import "ToDoCell.h"

@implementation ToDoCell

@end

ToDoViewController.m

#import "ToDoViewController.h"
#import "ToDoCell.h"

@interface ToDoViewController ()

@property NSMutableArray *toDoItems;

@end

@implementation ToDoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.toDoItems = [[NSMutableArray alloc] init];
    [self loadInitialData];
}

- (void)loadInitialData {
    NSString *item1 = @"Testing";
    [self.toDoItems addObject:item1];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

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

    cell.taskLabel.text = @"Testing";
    NSLog(@"Fudge Monkeys: %@", cell.taskLabel.text);

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

@end
12
  • Did you connect taskLabel with nib. Or add taskLabel to cell? If you use cell nib, remove init. If not (using code), add it to cell after init.
    – tuledev
    Aug 19, 2015 at 8:09
  • why you are intilizing them Aug 19, 2015 at 8:09
  • Just create an iBOutlet and use it Aug 19, 2015 at 8:10
  • just don't init the labels! (that will overwrite your xib, you only do that when you're not using xibs)
    – tafh
    Aug 19, 2015 at 8:11
  • I commented out the initializations, and now all of the UILabels are null.
    – Zhouster
    Aug 19, 2015 at 8:12

5 Answers 5

5

Issue is you are creating new objects for labels but then you are not adding them to superview but calling [self addSubview:label] ssigning them to properties does not add them to super view.

But this is totally unnecessary you should not init them in awakeNib. , and one thing more I will say about your code now you don't need to use @synthesis anymore as well. Any particular reason you are making IBOutlets strong?

Update you need to register nib with tableView first add following lines in viewDidLoad

[tableView registerNib:[UINib nibWithNibName:@"ToDoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ToDoCell"];

You can read more about this here

7
  • By "use them," do you mean to just directly reference the UILabel variables?
    – Zhouster
    Aug 19, 2015 at 8:15
  • 1
    Just FYI, didn't mean to copy your answer. There is just 6 seconds difference. Aug 19, 2015 at 8:29
  • 2
    When you are defining a cell in a nib, then before decqueue you have to register that nib with tableView, and on dequeue if its first time it will create an object automatically, other wise you have to create cell and your self. Aug 19, 2015 at 8:50
  • 2
    It is telling your tableview that it supposed to load its cells from a nib file and the cell identifier will be "ToDoCell".
    – Natasha
    Aug 19, 2015 at 8:52
  • 1
    Okay, that makes sense. Thank you very much for the help!
    – Zhouster
    Aug 19, 2015 at 8:56
2

If the UILabel instances are connected in Interface Builder via IBOutlet, you must not initialize them explicitly.

Just delete the complete awakeFromNib() method

3
  • Even after deleting the method, when I log the label, it says it's null. Why may that be?
    – Zhouster
    Aug 19, 2015 at 8:19
  • is cellForRowAtIndexPath called at all? Where do you reload the table data?
    – vadian
    Aug 19, 2015 at 8:23
  • Yes, it is called. It's called during an unwind segue (that I left out of the displayed code). Also, the log files show up with a (null) for the labels.
    – Zhouster
    Aug 19, 2015 at 8:25
1

Add this in viewDidLoad

NSString *strName = NSStringFromClass([ToDoCell class]);
[self.toDoItems registerNib:[UINib nibWithNibName:strName bundle:nil] forCellReuseIdentifier:strName];

You are using xib file but not loading it by registering it for the tableView. This is required when you use separate xib file. If you do not register, then only class will load and xib won't. And your awakeFromNib won't be called.

1

First of all delete awakeFromNib and make sure that you gave Cell Identifier in cell nib file.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *cellIdentifier = @"ToDoCell";
     ToDoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

     if (!cell)
         cell = [[ToDoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
     cell.taskLabel.text = @"Testing";
     return cell;
 }

Don't forget to add following code for register nib.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.tableView registerNib:[UINib nibWithNibName:@"ToDoCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"ToDoCell"];
}

Here is the sample code for your problem.

May this help to solve your problem.

0

You .xib why cell should be set through:

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    ToDoCell *cell = [tableView dequeueReusableCellWithIdentifier:[ToDoCell reuseIdentifier]];
    if (!cell) {
        cell = (ToDoCell *)
               [[NSBundle mainBundle] loadNibNamed:@"ToDoCell" owner:self options:nil].firstObject;
    }

    cell.taskLabel.text = @"Testing";
    NSLog(@"Fudge Monkeys: %@", cell.taskLabel.text);

    return cell;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.