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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With